标签:
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 |
#include <iostream> #include <cstring> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #define inf 10000000 #define mod 10000 typedef long long ll; using namespace std; const int N=105; const int M=50000; int power(int a,int b,int c){int ans=1;while(b){if(b%2==1){ans=(ans*a)%c;b--;}b/=2;a=a*a%c;}return ans;} int color[N], vis[N]; vector<int> G[N]; void dfs(int u) { vis[u] = 1; for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (!vis[v]) { color[v] = 3 - color[u]; dfs(v); } } } int main() { int n, t; scanf("%d", &n); for (int i = 1; i <= n; ++i) while (scanf("%d", &t) && t) { G[i].push_back(t); } memset(vis, 0, sizeof(vis)); memset(color, 0, sizeof(color)); for (int i = 1; i <= n; ++i) if (!vis[i]) { color[i]=1; dfs(i); } int sum = 0; for (int i = 1; i <= n; ++i) if (color[i] == 1) ++sum; printf("%d\n", sum); for (int i = 1; i <= n; ++i) if (color[i] == 1) printf("%d ", i); return 0; }
标签:
原文地址:http://www.cnblogs.com/jianrenfang/p/5850144.html