//============================================================================ // Name : .cpp // Author : Grzegorz Gajoch // Description : //============================================================================ #include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> #include <ctime> #include <ctype.h> #include <algorithm> #include <string> #include <string.h> #include <cstring> #include <vector> #include <stack> #include <queue> #include <map> #include <set> #include <list> using namespace std; #define FOR(i,a,b) for(int i = a; i <= b; ++i) #define FORD(i,a,b) for(int i = a; i >= b; --i) #define REP(x, n) for(int x=0; x < (n); ++x) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define SIZE(n) (int)n.size() #define ALL(c) c.begin(),c.end() #define PB(n) push_back(n) typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<vector<int> > VVI; typedef vector<bool> VB; #define MP make_pair #define ST first #define ND second const int INF = 1000000001; #undef DEBUG #ifdef DEBUG #define debug printf #else #define debug #endif int V, E, D; vector< set < int > > Nodes; vector< int > Connections; set< pair<int, int> > ConnSet; // (connections, node) vector< int > vis; vector<int> result; void print() { FOR(i, 1, V) { debug("Node %d: %d | ", i, Connections[i]); FOREACH(it, Nodes[i]) { debug("%d ", *it); } debug("\n"); } debug("ConnSet:\n"); FOREACH(it, ConnSet) { debug("%d -> %d\n", it->second, it->first); } } void resize() { Nodes.resize(V+1); Connections.resize(V+1, 0); vis.resize(V+1); } void clear_vis() { FOREACH(it, vis) { *it = 0; } } void read() { scanf("%d %d %d", &V, &E, &D); resize(); REP(i, E) { int a, b; scanf("%d %d", &a, &b); if( a == b ) { printf("EQUAL INPUTS!!!!!!\n"); exit(-1); } Nodes[a].insert(b); Nodes[b].insert(a); Connections[a]++; Connections[b]++; } FOR(i, 1, V) { ConnSet.insert(MP(Connections[i], i)); } } void delete_node(int nr) { debug("Deleting %d\n", nr); FOREACH(it, Nodes[nr]) { int dest = *it; debug("Conn (%d, %d)\n", nr, dest); Nodes[dest].erase(nr); ConnSet.erase(MP(Connections[dest], dest)); Connections[dest]--; ConnSet.insert(MP(Connections[dest], dest)); } ConnSet.erase(MP(Connections[nr], nr)); Connections[nr] = 0; Nodes[nr].clear(); } pair<int, int> take_min() { pair<int, int> P = *ConnSet.begin(); return P; } void dfs(int node, int value) { //debug("dfs: %d -> %d\n", node, value); vis[node] = value; FOREACH(it, Nodes[node]) { if( vis[*it] == 0 ) { dfs(*it, value); } } } void start_dfs(int start, int value) { std::stack<int> Dfs; Dfs.push(start); do { int top = Dfs.top(); Dfs.pop(); vis[top] = value; FOREACH(it, Nodes[top]) { if( vis[*it] == 0 ) { Dfs.push(*it); } } } while (!Dfs.empty()); } int do_dfs() { clear_vis(); int v = 1; FOR(i, 1, V) { if( vis[i] == 0 && Connections[i] > 0 ) { debug("DFS start node %d -> %d\n", i, v); dfs(i, v); //start_dfs(i, v); v++; } } return v; } int main(int argc, char **argv) { read(); //print(); pair<int, int> p = take_min(); while( p.first < D && !ConnSet.empty() ) { debug("smaller: %d -> %d\n", p.second, p.first); delete_node(p.second); #ifdef DEBUG //print(); #endif p = take_min(); } if( ConnSet.empty() ) { printf("NIE\n"); } else { int s = do_dfs(); debug("subsets: %d\n", s); vector<int> subset_sizes; subset_sizes.resize(s+1, 0); FOR(i, 1, V) { subset_sizes[vis[i]]++; debug("%d in subset %d\n", i, vis[i]); } //pair<int, int> subset_max = MP(0, 0); //size, number int max_size = 0, max_nr = 0; FOR(i, 1, s) { debug("subset %d -> %d nodes\n", i, subset_sizes[i]); if( subset_sizes[i] > max_size ) { max_nr = i; max_size = subset_sizes[i]; } } debug("max subset: %d -> %d\n", max_nr, max_size); FOR(i, 1, V) { if( Connections[i] >= D && vis[i] == max_nr ) { result.push_back(i); } } sort(ALL(result)); printf("%d\n", result.size()); FOREACH(it, result) { printf("%d ", *it); } 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 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | //============================================================================ // Name : .cpp // Author : Grzegorz Gajoch // Description : //============================================================================ #include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> #include <ctime> #include <ctype.h> #include <algorithm> #include <string> #include <string.h> #include <cstring> #include <vector> #include <stack> #include <queue> #include <map> #include <set> #include <list> using namespace std; #define FOR(i,a,b) for(int i = a; i <= b; ++i) #define FORD(i,a,b) for(int i = a; i >= b; --i) #define REP(x, n) for(int x=0; x < (n); ++x) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define SIZE(n) (int)n.size() #define ALL(c) c.begin(),c.end() #define PB(n) push_back(n) typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<vector<int> > VVI; typedef vector<bool> VB; #define MP make_pair #define ST first #define ND second const int INF = 1000000001; #undef DEBUG #ifdef DEBUG #define debug printf #else #define debug #endif int V, E, D; vector< set < int > > Nodes; vector< int > Connections; set< pair<int, int> > ConnSet; // (connections, node) vector< int > vis; vector<int> result; void print() { FOR(i, 1, V) { debug("Node %d: %d | ", i, Connections[i]); FOREACH(it, Nodes[i]) { debug("%d ", *it); } debug("\n"); } debug("ConnSet:\n"); FOREACH(it, ConnSet) { debug("%d -> %d\n", it->second, it->first); } } void resize() { Nodes.resize(V+1); Connections.resize(V+1, 0); vis.resize(V+1); } void clear_vis() { FOREACH(it, vis) { *it = 0; } } void read() { scanf("%d %d %d", &V, &E, &D); resize(); REP(i, E) { int a, b; scanf("%d %d", &a, &b); if( a == b ) { printf("EQUAL INPUTS!!!!!!\n"); exit(-1); } Nodes[a].insert(b); Nodes[b].insert(a); Connections[a]++; Connections[b]++; } FOR(i, 1, V) { ConnSet.insert(MP(Connections[i], i)); } } void delete_node(int nr) { debug("Deleting %d\n", nr); FOREACH(it, Nodes[nr]) { int dest = *it; debug("Conn (%d, %d)\n", nr, dest); Nodes[dest].erase(nr); ConnSet.erase(MP(Connections[dest], dest)); Connections[dest]--; ConnSet.insert(MP(Connections[dest], dest)); } ConnSet.erase(MP(Connections[nr], nr)); Connections[nr] = 0; Nodes[nr].clear(); } pair<int, int> take_min() { pair<int, int> P = *ConnSet.begin(); return P; } void dfs(int node, int value) { //debug("dfs: %d -> %d\n", node, value); vis[node] = value; FOREACH(it, Nodes[node]) { if( vis[*it] == 0 ) { dfs(*it, value); } } } void start_dfs(int start, int value) { std::stack<int> Dfs; Dfs.push(start); do { int top = Dfs.top(); Dfs.pop(); vis[top] = value; FOREACH(it, Nodes[top]) { if( vis[*it] == 0 ) { Dfs.push(*it); } } } while (!Dfs.empty()); } int do_dfs() { clear_vis(); int v = 1; FOR(i, 1, V) { if( vis[i] == 0 && Connections[i] > 0 ) { debug("DFS start node %d -> %d\n", i, v); dfs(i, v); //start_dfs(i, v); v++; } } return v; } int main(int argc, char **argv) { read(); //print(); pair<int, int> p = take_min(); while( p.first < D && !ConnSet.empty() ) { debug("smaller: %d -> %d\n", p.second, p.first); delete_node(p.second); #ifdef DEBUG //print(); #endif p = take_min(); } if( ConnSet.empty() ) { printf("NIE\n"); } else { int s = do_dfs(); debug("subsets: %d\n", s); vector<int> subset_sizes; subset_sizes.resize(s+1, 0); FOR(i, 1, V) { subset_sizes[vis[i]]++; debug("%d in subset %d\n", i, vis[i]); } //pair<int, int> subset_max = MP(0, 0); //size, number int max_size = 0, max_nr = 0; FOR(i, 1, s) { debug("subset %d -> %d nodes\n", i, subset_sizes[i]); if( subset_sizes[i] > max_size ) { max_nr = i; max_size = subset_sizes[i]; } } debug("max subset: %d -> %d\n", max_nr, max_size); FOR(i, 1, V) { if( Connections[i] >= D && vis[i] == max_nr ) { result.push_back(i); } } sort(ALL(result)); printf("%d\n", result.size()); FOREACH(it, result) { printf("%d ", *it); } printf("\n"); } } |