#include "sabotaz.h" #include "message.h" #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int maxn = 200200; int NODES, ID; int N,M; int res; // Linie oznaczone [D], [M], [A], [L] potrzebne tylko, jeśli szukamy // numeracji [D]wuspójnych, [M]ostów, p.[A]rtykulacji, funkcji [L]ow // mosty traktowane jako dwuspójne struct Edge { // PRE: musi być bcc = -1 i bridge = 0 (konstruktor ustawia) Edge* rev; int id; int dest; int bcc; // OUT: Numer komponentu bool bridge; // OUT: Czy most // M Edge(int v) : dest(v), bcc(-1) , bridge(0) // M {}; }; // IN: Liczba wierzchołków list<Edge> adj[maxn]; // IN: Listy sąsiedztwa int visit[maxn]; int bcc_num; // OUT: Liczba komponentów // D void add_edge (int a, int b) { //printf("dodaje krawedz %d %d\n", a, b); if (a==b) return; adj[a].pb(Edge(b)); adj[b].pb(Edge(a)); adj[a].back().rev = &adj[b].back(); adj[b].back().rev = &adj[a].back(); } stack<Edge*> st; // D int dfsTime; int bccDFS (int v, bool root = 0) { int lo = visit[v] = ++dfsTime; FOREACH(it,adj[v]) { if (it->bcc != -1) continue; st.push(&*it); // D it->rev->bcc = -2; if (!visit[it->dest]) { int ulo = bccDFS(it->dest); lo = min(ulo, lo); it->bridge = it->rev->bridge = (ulo > visit[v]); // M if (ulo >= visit[v]) { // AD Edge* edge; // D do { // D edge = st.top(); st.pop(); // D edge->bcc = edge->rev->bcc = bcc_num; // D } while (edge != &*it); // D ++bcc_num; // D } // AD } else lo = min(lo, visit[it->dest]); } return lo; } void computeBCC(){ fill(visit, visit+N+1, 0); dfsTime = 1; bcc_num = 0; // D FOR(i,N) if(!visit[i]) bccDFS(i, 1); // byc moze zmienic FORI na FOR } void inedges() { int first = 1LL*M*ID/NODES; int last = 1LL*M*(ID+1)/NODES; REP(i,first,last-1) { int a = BridgeEndA(i); int b = BridgeEndB(i); //printf("krawedz nr %d\n", i); add_edge(a,b); } } vi bcc_occ[maxn]; void solve() { computeBCC(); //printf("policzylem, mam %d dwuspojnych\n", bcc_num); FOR(i,bcc_num) bcc_occ[i].clear(); FOR(i,N) FOREACH(it,adj[i]) { bcc_occ[it->bcc].pb(i); } FOR(i,N) adj[i].clear(); FOR(i,bcc_num) { sort(bcc_occ[i].begin(), bcc_occ[i].end()); int nn=bcc_occ[i].size(); if (nn==1) continue; if (nn==2) { add_edge(bcc_occ[i][0], bcc_occ[i][1]); continue; } bcc_occ[i].pb(bcc_occ[i][0]); FOR(j,nn) if (bcc_occ[i][j] != bcc_occ[i][j+1]) add_edge(bcc_occ[i][j], bcc_occ[i][j+1]); } } const int MSGLIM = 19444; void sendgraph(int who) { int counter = 0; //printf("wysylam do %d\n", who); FOR(i,N) { int k=0; FOREACH(j,adj[i]) if (i<j->dest) k++; if (counter == MSGLIM) { Send(who); counter = 0; } PutInt(who, k); counter++; FOREACH(j,adj[i]) if (i<j->dest) { if (counter == MSGLIM) { Send(who); counter = 0; } PutInt(who, j->dest); counter++; //printf("wysylam %d %d\n", i, j->dest); } } Send(who); } void getgraph(int who) { int counter = 0; //printf("odbieram od %d\n", who); Receive(who); FOR(i,N) { if (counter == MSGLIM) { Receive(who); counter = 0; } int k = GetInt(who); counter++; FOR(j,k) { if (counter == MSGLIM) { Receive(who); counter = 0; } int tt = GetInt(who); counter++; add_edge(i,tt); } } } void bridges() { computeBCC(); FOR(i,N) FOREACH(it,adj[i]) if (it->bridge) res++; } void bruteforce() { FOR(i,N) adj[i].clear(); FOR(i,M) { int a = BridgeEndA(i); int b = BridgeEndB(i); //printf("krawedz nr %d\n", i); add_edge(a,b); } computeBCC(); FOR(i,N) FOREACH(it,adj[i]) if (it->bridge) { res++; printf("konce %d %d\n", i, it->dest); } } int main () { NODES = NumberOfNodes(); ID = MyNodeId(); N = NumberOfIsles(); M = NumberOfBridges(); inedges(); solve(); int numnodes = NODES; while (numnodes > 1) { int halfnum = (numnodes + 1) / 2; if (ID < halfnum) { // odbieram? if (ID+halfnum < numnodes) getgraph(ID+halfnum); } else { // wysylam sendgraph(ID-halfnum); return 0; } numnodes = halfnum; solve(); } bridges(); printf("%d\n", res/2); return 0; res=0; bruteforce(); printf("%d\n", res/2); } /* [0 0] [3 6] [7 5] [3 5] [6 2] [9 1] [2 7] [0 9] [3 6] [0 6] [2 6] [1 8] [7 9] [2 0] [2 3] [7 5] [9 2] [2 8] [3 7] [9 6] [0 0] [3 6] [7 5] [3 5] [6 2] [9 1] [2 7] [0 9] [3 6] [0 6] [2 6] [1 8] [7 9] [2 0] [2 3] [7 5] [9 2] [2 8] [9 7] [3 6] */
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 215 216 217 218 219 220 221 222 223 224 225 226 227 | #include "sabotaz.h" #include "message.h" #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int maxn = 200200; int NODES, ID; int N,M; int res; // Linie oznaczone [D], [M], [A], [L] potrzebne tylko, jeśli szukamy // numeracji [D]wuspójnych, [M]ostów, p.[A]rtykulacji, funkcji [L]ow // mosty traktowane jako dwuspójne struct Edge { // PRE: musi być bcc = -1 i bridge = 0 (konstruktor ustawia) Edge* rev; int id; int dest; int bcc; // OUT: Numer komponentu bool bridge; // OUT: Czy most // M Edge(int v) : dest(v), bcc(-1) , bridge(0) // M {}; }; // IN: Liczba wierzchołków list<Edge> adj[maxn]; // IN: Listy sąsiedztwa int visit[maxn]; int bcc_num; // OUT: Liczba komponentów // D void add_edge (int a, int b) { //printf("dodaje krawedz %d %d\n", a, b); if (a==b) return; adj[a].pb(Edge(b)); adj[b].pb(Edge(a)); adj[a].back().rev = &adj[b].back(); adj[b].back().rev = &adj[a].back(); } stack<Edge*> st; // D int dfsTime; int bccDFS (int v, bool root = 0) { int lo = visit[v] = ++dfsTime; FOREACH(it,adj[v]) { if (it->bcc != -1) continue; st.push(&*it); // D it->rev->bcc = -2; if (!visit[it->dest]) { int ulo = bccDFS(it->dest); lo = min(ulo, lo); it->bridge = it->rev->bridge = (ulo > visit[v]); // M if (ulo >= visit[v]) { // AD Edge* edge; // D do { // D edge = st.top(); st.pop(); // D edge->bcc = edge->rev->bcc = bcc_num; // D } while (edge != &*it); // D ++bcc_num; // D } // AD } else lo = min(lo, visit[it->dest]); } return lo; } void computeBCC(){ fill(visit, visit+N+1, 0); dfsTime = 1; bcc_num = 0; // D FOR(i,N) if(!visit[i]) bccDFS(i, 1); // byc moze zmienic FORI na FOR } void inedges() { int first = 1LL*M*ID/NODES; int last = 1LL*M*(ID+1)/NODES; REP(i,first,last-1) { int a = BridgeEndA(i); int b = BridgeEndB(i); //printf("krawedz nr %d\n", i); add_edge(a,b); } } vi bcc_occ[maxn]; void solve() { computeBCC(); //printf("policzylem, mam %d dwuspojnych\n", bcc_num); FOR(i,bcc_num) bcc_occ[i].clear(); FOR(i,N) FOREACH(it,adj[i]) { bcc_occ[it->bcc].pb(i); } FOR(i,N) adj[i].clear(); FOR(i,bcc_num) { sort(bcc_occ[i].begin(), bcc_occ[i].end()); int nn=bcc_occ[i].size(); if (nn==1) continue; if (nn==2) { add_edge(bcc_occ[i][0], bcc_occ[i][1]); continue; } bcc_occ[i].pb(bcc_occ[i][0]); FOR(j,nn) if (bcc_occ[i][j] != bcc_occ[i][j+1]) add_edge(bcc_occ[i][j], bcc_occ[i][j+1]); } } const int MSGLIM = 19444; void sendgraph(int who) { int counter = 0; //printf("wysylam do %d\n", who); FOR(i,N) { int k=0; FOREACH(j,adj[i]) if (i<j->dest) k++; if (counter == MSGLIM) { Send(who); counter = 0; } PutInt(who, k); counter++; FOREACH(j,adj[i]) if (i<j->dest) { if (counter == MSGLIM) { Send(who); counter = 0; } PutInt(who, j->dest); counter++; //printf("wysylam %d %d\n", i, j->dest); } } Send(who); } void getgraph(int who) { int counter = 0; //printf("odbieram od %d\n", who); Receive(who); FOR(i,N) { if (counter == MSGLIM) { Receive(who); counter = 0; } int k = GetInt(who); counter++; FOR(j,k) { if (counter == MSGLIM) { Receive(who); counter = 0; } int tt = GetInt(who); counter++; add_edge(i,tt); } } } void bridges() { computeBCC(); FOR(i,N) FOREACH(it,adj[i]) if (it->bridge) res++; } void bruteforce() { FOR(i,N) adj[i].clear(); FOR(i,M) { int a = BridgeEndA(i); int b = BridgeEndB(i); //printf("krawedz nr %d\n", i); add_edge(a,b); } computeBCC(); FOR(i,N) FOREACH(it,adj[i]) if (it->bridge) { res++; printf("konce %d %d\n", i, it->dest); } } int main () { NODES = NumberOfNodes(); ID = MyNodeId(); N = NumberOfIsles(); M = NumberOfBridges(); inedges(); solve(); int numnodes = NODES; while (numnodes > 1) { int halfnum = (numnodes + 1) / 2; if (ID < halfnum) { // odbieram? if (ID+halfnum < numnodes) getgraph(ID+halfnum); } else { // wysylam sendgraph(ID-halfnum); return 0; } numnodes = halfnum; solve(); } bridges(); printf("%d\n", res/2); return 0; res=0; bruteforce(); printf("%d\n", res/2); } /* [0 0] [3 6] [7 5] [3 5] [6 2] [9 1] [2 7] [0 9] [3 6] [0 6] [2 6] [1 8] [7 9] [2 0] [2 3] [7 5] [9 2] [2 8] [3 7] [9 6] [0 0] [3 6] [7 5] [3 5] [6 2] [9 1] [2 7] [0 9] [3 6] [0 6] [2 6] [1 8] [7 9] [2 0] [2 3] [7 5] [9 2] [2 8] [9 7] [3 6] */ |