Advertisement
lmarioza

Untitled

Sep 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAXN 100000
  3. #define mp make_pair
  4. #define pb push_back
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int n; // Vertices
  10. int G[MAXN][MAXN]; // Grafo
  11. //vector<int> grafo[MAXN];
  12. //map<pair<int,int>,int> grafo;
  13. //set<int> bip[2];
  14. int C[MAXN],vis[MAXN]; // Cores
  15.  
  16. int counter[MAXN];
  17. int cor[3];
  18.  
  19.  
  20.  
  21. bool bipartite(int v, int c) {
  22.     C[v] = c;
  23.     cor[c]++;
  24.     int a = c;
  25.     bool ret = true;
  26.     c = c == 1? 2 : 1;
  27.     for (int u = 0; u < n; u++) {
  28.         if (G[v][u])  {
  29.             if (!C[u]) ret = bipartite(u, c);
  30.             else if (C[u] == a) return false;
  31.             if (!ret) return false;
  32.         }else{
  33.             grafo.erase(mp(v,u));
  34.         }
  35.     }
  36.     return ret;
  37. }
  38.  
  39. int main(){
  40.    
  41.     scanf("%d",&n);
  42.     memset(counter,0,sizeof counter);
  43.     for(int i=0;i<n-1;i++){
  44.         int x,y;
  45.         scanf("%d %d",&x,&y);
  46.         x--;
  47.         y--;
  48.  
  49.         //grafo[x-1].pb(y-1);
  50.         //grafo[y-1].pb(x-1);
  51.         G[x][y]=true;
  52.         G[y][x]=true;
  53.         //grafo[mp(x-1,y-1)] = 1;
  54.         //grafo[mp(y-1,x-1)] = 1;
  55.         if(!C[x]){
  56.  
  57.         }
  58.         counter[x]++;
  59.         counter[y]++;
  60.     }
  61.     cor[0] = cor[1] = cor[2] = 0;
  62.  
  63.     for(int i=0;i<n;i++)
  64.         if(!C[i])
  65.             bipartite(i,1);
  66.  
  67.     //printf("%d %d %d\n",cor[0],cor[1],cor[2] );
  68.     int ans1=0,ans2 =0;
  69.     for(int i=0;i<n;i++){
  70.         //printf("%d %d %d\n",C[i],cor[(C[i]%2)+1],counter[i] );
  71.         if(C[i] == 1){
  72.             ans1+= (cor[2] - counter[i]);
  73.         }else{
  74.             ans2+=(cor[1] - counter[i]);
  75.         }
  76.     }
  77.     printf("%d\n",max(ans1,ans2) );
  78.    
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement