Tuesday, February 7, 2017

1012 - Guilty Prince - LOJ


#include <bits/stdc++.h>
#define sf scanf
#define pf printf
#define newl '\n'
#define FAST ios_base::sync_with_stdio(0);
#define Case "Case "<<++kase<<": "
#define GRAY 0
#define WHITE 1
#define BLACK 2
using namespace std;

int main()
{
   int T,kase=0;
   cin >> T;
   while(T--){
    int Edgdes;
    cin >> Edgdes;
    vector<int>adj[20000+1];
    int color[20000+1];
    memset(color,GRAY,sizeof color);
    for(int i=0; i<Edgdes; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    int mx = 0;
    for(int i=0; i<20000+1; i++){
        if((adj[i].size()!=0) && (color[i] == GRAY)){
            int  black=0,white=0;
            queue<int>q;
            q.push(i);
            color[i] = BLACK;
            black++;
            while(!q.empty()){
                int front = q.front();
                q.pop();
                for(int j=0; j<adj[front].size(); j++){
                    if(color[adj[front][j]] == GRAY){
                       
                        if(color[front] == BLACK){
                            color[adj[front][j]] = WHITE;
                            white++;
                        }else if(color[front] == WHITE){
                            color[adj[front][j]] = BLACK;
                            black++;
                        }
                      
                        q.push(adj[front][j]);
                    }
                }
            }
            mx += max(black,white);
        }
    }
    cout <<Case<<mx<<newl;
   }
    return 0;
}

No comments:

Post a Comment