#include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <list> using namespace std; #define MX 200000 struct Edge { int v; Edge* prev; Edge* next; Edge* other; Edge(): v(0), prev(NULL), next(NULL), other(NULL) {}; Edge(int _v): v(_v), prev(NULL), next(NULL), other(NULL) {}; }; struct List{ Edge first; int size; List(): size(0) {}; }; List graph[MX]; vector<int> toRemove; bool removed[MX]; void addEdge( List* l, Edge* e ){ Edge* next = l->first.next; l->first.next = e; e->next = next; if( next != NULL ) next->prev = e; e->prev = &l->first; l->size++; } void remove( List* l, Edge* e ) { Edge* prevE = e->prev; Edge* nextE = e->next; prevE->next = nextE; if( nextE != NULL ) nextE->prev = prevE; l->size--; } void removeVertex( int v, int d ) { Edge* firstElem = graph[v].first.next; while( firstElem != NULL ) { remove( &graph[firstElem->v], firstElem->other ); if(( !removed[firstElem->v] ) && ( graph[firstElem->v].size < d )) { removed[firstElem->v] = true; toRemove.push_back(firstElem->v); } firstElem = firstElem->next; } graph[v].first.next = NULL; } int visited[MX]; vector<int> cnt; void dfs( int n ) { visited[n] = cnt.size(); cnt.back()++; Edge* first = graph[n].first.next; while( first != NULL ) { if( !visited[first->v] ) dfs( first->v ); first = first->next; } } int n, m, d; int main() { scanf( "%d%d%d", &n, &m, &d ); for( int i = 0 ; i < m; i++ ) { int a, b; scanf( "%d%d", &a, &b ); Edge* f = (Edge*)calloc( sizeof(Edge), 1 ); Edge* s = (Edge*)calloc( sizeof(Edge), 1 ); f->v = --a; f->other = s; addEdge( &graph[--b], f ); s->v = b; s->other = f; addEdge( &graph[a], s ); } for( int i = 0; i < n; i++ ) { if( graph[i].size < d ) { removed[i] == true; toRemove.push_back(i); } } while( toRemove.size() > 0 ) { int a = toRemove.back(); toRemove.pop_back(); removeVertex( a, d ); } for( int i = 0 ; i < n; i++ ) { if( !visited[i] ) { cnt.push_back(0); dfs(i); } } int maxRes = 0; int maxI = 0; for( int i = 0; i<cnt.size(); i++ ) { if(cnt[i]>maxRes) { maxRes = cnt[i]; maxI = i + 1; }; } if( maxRes == 1 ) { printf( "NIE\n" ); } else { printf( "%d\n", maxRes ); for( int i = 0 ; i < n; i++ ) { if( visited[i] == maxI ) printf( "%d ", i + 1 ); } printf( "\n" ); } /* printf results */ 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <list> using namespace std; #define MX 200000 struct Edge { int v; Edge* prev; Edge* next; Edge* other; Edge(): v(0), prev(NULL), next(NULL), other(NULL) {}; Edge(int _v): v(_v), prev(NULL), next(NULL), other(NULL) {}; }; struct List{ Edge first; int size; List(): size(0) {}; }; List graph[MX]; vector<int> toRemove; bool removed[MX]; void addEdge( List* l, Edge* e ){ Edge* next = l->first.next; l->first.next = e; e->next = next; if( next != NULL ) next->prev = e; e->prev = &l->first; l->size++; } void remove( List* l, Edge* e ) { Edge* prevE = e->prev; Edge* nextE = e->next; prevE->next = nextE; if( nextE != NULL ) nextE->prev = prevE; l->size--; } void removeVertex( int v, int d ) { Edge* firstElem = graph[v].first.next; while( firstElem != NULL ) { remove( &graph[firstElem->v], firstElem->other ); if(( !removed[firstElem->v] ) && ( graph[firstElem->v].size < d )) { removed[firstElem->v] = true; toRemove.push_back(firstElem->v); } firstElem = firstElem->next; } graph[v].first.next = NULL; } int visited[MX]; vector<int> cnt; void dfs( int n ) { visited[n] = cnt.size(); cnt.back()++; Edge* first = graph[n].first.next; while( first != NULL ) { if( !visited[first->v] ) dfs( first->v ); first = first->next; } } int n, m, d; int main() { scanf( "%d%d%d", &n, &m, &d ); for( int i = 0 ; i < m; i++ ) { int a, b; scanf( "%d%d", &a, &b ); Edge* f = (Edge*)calloc( sizeof(Edge), 1 ); Edge* s = (Edge*)calloc( sizeof(Edge), 1 ); f->v = --a; f->other = s; addEdge( &graph[--b], f ); s->v = b; s->other = f; addEdge( &graph[a], s ); } for( int i = 0; i < n; i++ ) { if( graph[i].size < d ) { removed[i] == true; toRemove.push_back(i); } } while( toRemove.size() > 0 ) { int a = toRemove.back(); toRemove.pop_back(); removeVertex( a, d ); } for( int i = 0 ; i < n; i++ ) { if( !visited[i] ) { cnt.push_back(0); dfs(i); } } int maxRes = 0; int maxI = 0; for( int i = 0; i<cnt.size(); i++ ) { if(cnt[i]>maxRes) { maxRes = cnt[i]; maxI = i + 1; }; } if( maxRes == 1 ) { printf( "NIE\n" ); } else { printf( "%d\n", maxRes ); for( int i = 0 ; i < n; i++ ) { if( visited[i] == maxI ) printf( "%d ", i + 1 ); } printf( "\n" ); } /* printf results */ return 0; } |