- /****************##### بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #####******************
- __________________________________________________________________________
- ###################### Ya-Seen Arafat(ACWizard) #########################
- ###################### UAP-CSE-33B #########################
- *************************************************************************/
- #include <bits/stdc++.h>
- #define sc(n) scanf("%d", &n)
- #define scD(n) scanf("%lf", &n)
- #define scS(n) scanf("%s", &n)
- #define S 20003
- #define SS 100003
- using namespace std;
- typedef long long LL;
- vector <int> race[S], edge;
- int ans[S], check[S];
- int BFS(int src){
- int anss = 0;
- queue <int> Q;
- Q.push(src);
- int sz = edge.size();
- ans[src] = 1;
- while(!Q.empty()){
- int x, y;
- x = Q.front();
- check[x]++;
- Q.pop();
- y = race[x].size();
- for(int i = 0; i < y; i++){
- if(!ans[race[x][i]]){
- Q.push(race[x][i]);
- if(ans[x] == 1)ans[race[x][i]] = 2;
- if(ans[x] == 2)ans[race[x][i]] = 1;
- }
- }
- if(Q.empty()){
- int vamp = 0, lyk = 0;
- for(int i = 0; i < S; i++){
- if(ans[i] == 1)vamp++;
- if(ans[i] == 2)lyk++;
- ans[i] = 0;
- }
- anss += max(vamp, lyk);
- for(int i = 0; i < sz; i++){
- if(check[edge[i]] == 1){
- Q.push(edge[i]);
- ans[edge[i]] = 1;
- break;
- }
- }
- }
- }
- return anss;
- }
- void Do(){
- int t, n, u, v, cs = 0;
- sc(t);
- while(t--){
- sc(n);
- memset(ans, 0, sizeof(ans));
- memset(check, 0, sizeof(check));
- for(int i = 0; i < S; i++)race[i].clear();
- edge.clear();
- while(n--){
- sc(u);
- sc(v);
- if(!check[u])check[u] = 1, edge.push_back(u);
- if(!check[v])check[v] = 1, edge.push_back(v);
- race[u].push_back(v);
- race[v].push_back(u);
- }
- int members = BFS(u);
- printf("Case %d: %d\n", ++cs, members);
- }
- }
- int main(){
- ios_base::sync_with_stdio(0); cin.tie(0);
- #ifndef ONLINE_JUDGE
- ///freopen("contest.txt","w",stdout);
- #endif
- Do();
- return 0;
- }
Saturday, January 9, 2016
LightOJ - 1009 - Back to Underworld --- using BFS
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment