#include <stdio.h> #include <set> #include <queue> #include <utility> using namespace std; class CompareDist { public: bool operator()(pair<int,int> n1,pair<int,int> n2) { return n1.second<n2.second; } }; set<int> *graph = new set<int>[200001]; //priority_queue<int> myqueue; priority_queue<pair<int,int>,vector<pair<int,int> >,CompareDist> myqueue; set<int>::iterator it; int colors[200001]; pair <int,int> foo; int colorize(int v,int color) { queue<int> myqueue2; myqueue2.push(v); int result = 0; while (!myqueue2.empty()) { v = myqueue2.front(); myqueue2.pop(); if (colors[v]!=0) continue; colors[v]=color; ++result; for (it=graph[v].begin(); it!=graph[v].end(); ++it) { myqueue2.push(*it); } } return result; } int main() { int n,m,d,a,b; int i,size; scanf("%d %d %d", &n, &m, &d); for(int i=0; i<m; ++i) { scanf("%d %d", &a, &b); graph[a].insert(b); graph[b].insert(a); } for(int i=1; i<=n; ++i) { if (graph[i].size()<d) { myqueue.push(make_pair(i,graph[i].size())); } } while (!myqueue.empty()) { foo = myqueue.top(); i = foo.first; myqueue.pop(); if (graph[i].size()<d) { for (it=graph[i].begin(); it!=graph[i].end(); ++it) { graph[*it].erase(i); myqueue.push(make_pair(*it, graph[*it].size())); } graph[i].clear(); } } //kolorowanie grafu for(int i=1; i<=n; ++i) { colors[i]=0; } int color=1,result; int best_result=0, best_color=0; for(int i=1; i<=n; ++i) { if (colors[i]==0) { result = colorize(i,color); if (result>=d && result>best_result) { best_result=result; best_color=color; } //printf("%d\n", result); color++; } } if (best_result==0) printf("NIE"); else { printf("%d\n", best_result); for(int i=1; i<=n; ++i) { if (colors[i]==best_color) printf("%d ", i); } } return 0; }
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 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include <stdio.h> #include <set> #include <queue> #include <utility> using namespace std; class CompareDist { public: bool operator()(pair<int,int> n1,pair<int,int> n2) { return n1.second<n2.second; } }; set<int> *graph = new set<int>[200001]; //priority_queue<int> myqueue; priority_queue<pair<int,int>,vector<pair<int,int> >,CompareDist> myqueue; set<int>::iterator it; int colors[200001]; pair <int,int> foo; int colorize(int v,int color) { queue<int> myqueue2; myqueue2.push(v); int result = 0; while (!myqueue2.empty()) { v = myqueue2.front(); myqueue2.pop(); if (colors[v]!=0) continue; colors[v]=color; ++result; for (it=graph[v].begin(); it!=graph[v].end(); ++it) { myqueue2.push(*it); } } return result; } int main() { int n,m,d,a,b; int i,size; scanf("%d %d %d", &n, &m, &d); for(int i=0; i<m; ++i) { scanf("%d %d", &a, &b); graph[a].insert(b); graph[b].insert(a); } for(int i=1; i<=n; ++i) { if (graph[i].size()<d) { myqueue.push(make_pair(i,graph[i].size())); } } while (!myqueue.empty()) { foo = myqueue.top(); i = foo.first; myqueue.pop(); if (graph[i].size()<d) { for (it=graph[i].begin(); it!=graph[i].end(); ++it) { graph[*it].erase(i); myqueue.push(make_pair(*it, graph[*it].size())); } graph[i].clear(); } } //kolorowanie grafu for(int i=1; i<=n; ++i) { colors[i]=0; } int color=1,result; int best_result=0, best_color=0; for(int i=1; i<=n; ++i) { if (colors[i]==0) { result = colorize(i,color); if (result>=d && result>best_result) { best_result=result; best_color=color; } //printf("%d\n", result); color++; } } if (best_result==0) printf("NIE"); else { printf("%d\n", best_result); for(int i=1; i<=n; ++i) { if (colors[i]==best_color) printf("%d ", i); } } return 0; } |