标签:
| input | output |
|---|---|
7 2 3 0 3 1 0 1 2 4 5 0 3 0 3 0 7 0 6 0 |
4 2 4 5 6 |
解析:貌似二分图。但是比二分图简单。
开两个标记数组,记录每个人是否有朋友在第一组,第二组。
扫一遍人:
1.若在第一组没有他的朋友:则将它放到第一组,并标记他的朋友在第一组都有朋友。
2.否则:
(1) 若在第二组没有他的朋友,则将它放到第二组,并标记他的朋友在第二组都有朋友。
(2)否则,说明在两组中都没有朋友,则可将他随便放在一个组里,也可不处理。
AC代码:
#include <bits/stdc++.h>
using namespace std;
bool c[105], b[105];
vector<int> a[105];
vector<int> ans;
int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif // sxk
int n, x;
while(scanf("%d", &n) != EOF){
memset(c, false, sizeof(c));
memset(b, false, sizeof(b));
ans.clear();
for(int i=1; i<=n; i++){
while(scanf("%d", &x) && x) a[i].push_back(x);
}
for(int i=1; i<=n; i++){
if(!c[i]){ //第一组没朋友
ans.push_back(i);
for(int j=0; j<a[i].size(); j++) c[ a[i][j] ] = true;
}
else{
if(!b[i]){ //第二组没朋友
for(int j=0; j<a[i].size(); j++) b[ a[i][j] ] = true;
} //都没有,不处理
}
}
int cnt = ans.size();
cnt %= n;
printf("%d\n", cnt);
for(int i=0; i<cnt; i++){
printf("%d%c", ans[i], i < cnt-1 ? ' ' : '\n');
}
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/u013446688/article/details/46572617