Advertisement
newb_ie

Untitled

Oct 31st, 2021
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5. typedef unsigned long long int ull;
  6. typedef vector<int> vi;
  7. typedef vector<ll>vl;
  8. typedef vector<vi>vii;
  9. typedef vector<vl>vll;
  10. typedef pair<int,int>pii;
  11. typedef double dl;
  12. typedef pair<dl,dl>pdd;
  13. typedef pair<ll,ll>pll;
  14.  
  15. #define PB push_back
  16. #define F first
  17. #define S second
  18. #define MP make_pair
  19. #define nl '\n'
  20. #define all(a)(a).begin(),(a).end()
  21. #define sz(x) (int)x.size()
  22. #define REP(i,m,n) for(int i=m;i<n;i++)
  23.  
  24. const double PI = acos(-1);
  25. const double eps = 1e-9;
  26.  
  27. #define mem(a,b) memeset(a,b,sizeof(a))
  28. #define gcd(a,b) __gcd(a,b)
  29. #define sqr(a) ((a)*(a))
  30. #define prof ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  31. #define fraction(a) cout.unsetf(ios::floatfield); cout.precision(a);cout.setf(ios::fixed,ios::floatfield);
  32. #define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
  33. const int mx=20100;
  34. vector<int>adj[mx];
  35. int color[mx];
  36. bool visited[mx];
  37. int cnt1=0,cnt2=0;
  38. bool dfs(int node,int col)
  39. {
  40. visited[node]=true;
  41. color[node]=col;
  42. if (col == 1) cnt1++;
  43. else cnt2++;
  44. for(auto u:adj[node])
  45. {
  46. if(visited[u]==false)
  47. {
  48. bool f=dfs(u,col==1?2:1);
  49. if(!f)
  50. return false;
  51. }
  52. else
  53. {
  54. if(color[node]==color[u])
  55. return false;
  56. }
  57.  
  58. }
  59. return true;
  60. }
  61. int main()
  62. {
  63. prof;
  64. int tc;
  65. cin>>tc;
  66. int cnt3=1;
  67. while(tc--)
  68. {
  69. int m;
  70. cin>>m;
  71. int cnt=0;
  72. for (int i = 1; i < mx; ++i) adj[i].clear();
  73. for (int i = 1; i < mx; ++i) visited[i] = false;
  74. for(int i=0; i<m; i++)
  75. {
  76. int u,v;
  77. cin>>u>>v;
  78. cnt=max(cnt,max(u,v));
  79. adj[u].push_back(v);
  80. adj[v].push_back(u);
  81. }
  82. int res = 0;
  83. for(int i=1; i<=cnt; i++)
  84. {
  85. if(visited[i]==false) {
  86. cnt1 = 0, cnt2 = 0;
  87. dfs(i,1);
  88. if (cnt1 == 0 or cnt2 == 0) cnt1 = 0, cnt2 = 0;
  89. res += max(cnt1,cnt2);
  90. }
  91. }
  92. cout<<"Case "<<cnt3<<": "<<res<<nl;
  93. cnt3++;
  94. }
  95.  
  96. return 0;
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement