#include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second list<int> adj[300000]; int deg[300000]; bool visited[300000]; int color[300000]; int dfs(int v, int c) { if (visited[v]) return 0; visited[v] = true; int result = 1; color[v] = c; FOREACH(it, adj[v]) result += dfs(*it, c); return result; } int main() { int N, M, D; scanf("%d%d%d", &N, &M, &D); REP(i,M) { int a, b; scanf("%d%d", &a, &b); --a, --b; adj[a].pb(b); adj[b].pb(a); ++deg[a]; ++deg[b]; } queue<int> Q; REP(i,N) if (deg[i] < D) Q.push(i); while (!Q.empty()) { int v = Q.front(); Q.pop(); FOREACH(it, adj[v]) { --deg[*it]; if (deg[*it] == D - 1) Q.push(*it); } } REP(i,N) { visited[i] = (deg[i] < D); color[i] = -1; } int result = 0, maxi = -1; REP(i,N) if (!visited[i]) { int tmp = dfs(i, i); if (tmp > result) { result = tmp; maxi = i; } } if (result == 0) printf("NIE\n"); else { printf("%d\n", result); REP(i,N) if (color[i] == maxi) printf("%d ", i + 1); printf("\n"); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second list<int> adj[300000]; int deg[300000]; bool visited[300000]; int color[300000]; int dfs(int v, int c) { if (visited[v]) return 0; visited[v] = true; int result = 1; color[v] = c; FOREACH(it, adj[v]) result += dfs(*it, c); return result; } int main() { int N, M, D; scanf("%d%d%d", &N, &M, &D); REP(i,M) { int a, b; scanf("%d%d", &a, &b); --a, --b; adj[a].pb(b); adj[b].pb(a); ++deg[a]; ++deg[b]; } queue<int> Q; REP(i,N) if (deg[i] < D) Q.push(i); while (!Q.empty()) { int v = Q.front(); Q.pop(); FOREACH(it, adj[v]) { --deg[*it]; if (deg[*it] == D - 1) Q.push(*it); } } REP(i,N) { visited[i] = (deg[i] < D); color[i] = -1; } int result = 0, maxi = -1; REP(i,N) if (!visited[i]) { int tmp = dfs(i, i); if (tmp > result) { result = tmp; maxi = i; } } if (result == 0) printf("NIE\n"); else { printf("%d\n", result); REP(i,N) if (color[i] == maxi) printf("%d ", i + 1); printf("\n"); } } |