timus 1106 - Two Teams solution: http://ideone.com/r8JprM
#include <iostream> #include <cstdio> using namespace std; int main() { int adj[105][105]={0}, b[105]={0}, g[105]={0}, f, cnt, flag=0, n; scanf("%d", &n); for(int i=0; i<n; i++) { cnt=0; while(true) { scanf("%d", &f); if(f==0 && cnt==0) {flag=1; break;} else if(f==0) break; adj[i][f-1]=1, cnt++; } } cnt=0; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(adj[i][j]==1 && b[j]==0) { b[i]=b[j]=1; if(g[i]==1) g[j]=2; else g[j]=1, cnt++; } } } if(flag==1) printf("0"); else { printf("%d\n", cnt); for(int i=0; i<n; i++) if(g[i]==1) printf("%d ", i+1); } return 0; }
note: dfs or bfs, i dont know which applies to solve this question. but in my case, it was like this: assign this guy to one of either groups, and assign its friends(if not assigned yet) to opposing group. adj[] array is adjacency list(you would have seen that while you were studying bfs). b[] is for bool, whether that vertex has been visited or not. g[] for group.
for other versions of solutions:
1 - https://github.com/marioyc/Online-Judge-Solutions/blob/master/Timus%20Online%20Judge/1106%20-%20Two%20Teams.cpp
2 - https://github.com/galymzhan/plzgosolve/blob/master/timus/1106.cpp
3 - http://blog.csdn.net/pegasuswang_/article/details/12394355
4 - https://code.google.com/p/morbidel-timus/source/browse/trunk/done/1106.cpp?r=15
No comments:
Post a Comment