#include <unordered_set> #include <unordered_map> #include <functional> #include <algorithm> //#include <iostream> #include <numeric> #include <cassert> #include <cstring> #include <cstdio> #include <vector> #include <queue> //#include <cmath> #include <set> #include <map> using namespace std; #include "message.h" #include "kollib.h" typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<LL> VLL; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b);i>=(e);--i) #define PB push_back #define ALL(V) (V).begin(),(V).end() #define SIZE(V) ((int)(V).size()) #define MP make_pair #define ST first #define ND second #define DBG #ifdef DBG #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif int __stmp; #define scanf __stmp=scanf const int INF = 1000000001; const int MAX = 100000; const int MAXM = 210; const int T = 10000; PII Q[MAXM]; int n, m; int main(int argc, char *argv[]) { int nodes = NumberOfNodes(); int id = MyNodeId(); n = NumberOfStudents(); m = NumberOfQueries(); unordered_set<int> S; REP(i,m) { Q[i] = PII(QueryFrom(i+1), QueryTo(i+1)); S.insert(Q[i].ST); S.insert(Q[i].ND); } mt19937_64 gen(4327822); uniform_int_distribution<int> rd(1, n); REP(foo,T) S.insert(rd(gen)); VI P(ALL(S)); for(int i=SIZE(P)-1;i>0;i--) swap(P[i], P[uniform_int_distribution<int>(0, i)(gen)]); unordered_map<int,PII> G[2]; for(int i=id;i<SIZE(P);i+=nodes) { if(id) { PutInt(0, P[i]); } REP(k,2) { int prv = P[i]; int u = !k ? FirstNeighbor(P[i]) : SecondNeighbor(P[i]); int d = 1; while(!S.count(u)) { d++; int t = u; u = FirstNeighbor(u) + SecondNeighbor(u) - prv; prv = t; } if(!id) { G[k][P[i]] = PII(u, d); } else { PutInt(0, u); PutInt(0, d); } } } if(id) { PutInt(0, 0); Send(0); return 0; } for(int s=1;s<nodes;s++) { Receive(s); for(int u=GetInt(s);u;u=GetInt(s)) { REP(k,2) { int v = GetInt(s); int d = GetInt(s); G[k][u] = PII(v, d); } } } REP(i,m) { int res = 0; int u = Q[i].ST; int prv = -1; while(u != Q[i].ND) { PII v1 = G[0][u]; PII v2 = G[1][u]; if(v1.ST == prv) swap(v1, v2); res += v1.ND; prv = u; u = v1.ST; } printf("%d\n", min(res, n-res)); } 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 | #include <unordered_set> #include <unordered_map> #include <functional> #include <algorithm> //#include <iostream> #include <numeric> #include <cassert> #include <cstring> #include <cstdio> #include <vector> #include <queue> //#include <cmath> #include <set> #include <map> using namespace std; #include "message.h" #include "kollib.h" typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<LL> VLL; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b);i>=(e);--i) #define PB push_back #define ALL(V) (V).begin(),(V).end() #define SIZE(V) ((int)(V).size()) #define MP make_pair #define ST first #define ND second #define DBG #ifdef DBG #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif int __stmp; #define scanf __stmp=scanf const int INF = 1000000001; const int MAX = 100000; const int MAXM = 210; const int T = 10000; PII Q[MAXM]; int n, m; int main(int argc, char *argv[]) { int nodes = NumberOfNodes(); int id = MyNodeId(); n = NumberOfStudents(); m = NumberOfQueries(); unordered_set<int> S; REP(i,m) { Q[i] = PII(QueryFrom(i+1), QueryTo(i+1)); S.insert(Q[i].ST); S.insert(Q[i].ND); } mt19937_64 gen(4327822); uniform_int_distribution<int> rd(1, n); REP(foo,T) S.insert(rd(gen)); VI P(ALL(S)); for(int i=SIZE(P)-1;i>0;i--) swap(P[i], P[uniform_int_distribution<int>(0, i)(gen)]); unordered_map<int,PII> G[2]; for(int i=id;i<SIZE(P);i+=nodes) { if(id) { PutInt(0, P[i]); } REP(k,2) { int prv = P[i]; int u = !k ? FirstNeighbor(P[i]) : SecondNeighbor(P[i]); int d = 1; while(!S.count(u)) { d++; int t = u; u = FirstNeighbor(u) + SecondNeighbor(u) - prv; prv = t; } if(!id) { G[k][P[i]] = PII(u, d); } else { PutInt(0, u); PutInt(0, d); } } } if(id) { PutInt(0, 0); Send(0); return 0; } for(int s=1;s<nodes;s++) { Receive(s); for(int u=GetInt(s);u;u=GetInt(s)) { REP(k,2) { int v = GetInt(s); int d = GetInt(s); G[k][u] = PII(v, d); } } } REP(i,m) { int res = 0; int u = Q[i].ST; int prv = -1; while(u != Q[i].ND) { PII v1 = G[0][u]; PII v2 = G[1][u]; if(v1.ST == prv) swap(v1, v2); res += v1.ND; prv = u; u = v1.ST; } printf("%d\n", min(res, n-res)); } return 0; } |