#include "kollib.h" #include "message.h" // Grzegorz Guspiel #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<int(n);++i) #define SIZE(c) ((int)((c).size())) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second template<typename T> void maxE(T& a, const T& b) { a = max(a, b); } template<typename T> void minE(T& a, const T& b) { a = min(a, b); } template<typename T> ostream& operator<<(ostream& out, const vector<T>& t) { out << "["; FOREACH (i, t) out << *i << ", "; out << "]"; return out; } typedef pair<int, int> PII; const int INF = 1000 * 1000 * 1000 + 1000; ofstream out; int nodes; int mother; unordered_set<int> special, mothers, queried; int firstNeighbor(int a) { return FirstNeighbor(a + 1) - 1; } int secondNeighbor(int a) { return SecondNeighbor(a + 1) - 1; } vector<int> go(int a, int b) { int steps = 0; bool isMother; vector<int> r; r.pb(a); do { steps++; bool isSpecial = special.find(b) != special.end(); isMother = 0; if (isSpecial) { isMother = (mothers.find(b) != mothers.end()); if (!isMother && queried.find(b) != queried.end()) { r.pb(steps); steps = 0; r.pb(b); } } int c = firstNeighbor(b); if (c == a) c = secondNeighbor(b); a = b; b = c; } while (!isMother); r.pb(steps); r.pb(a); return r; } void addQueried(int a) { assert(a < NumberOfStudents()); if (queried.find(a) == queried.end()) queried.insert(a); if (special.find(a) == special.end()) special.insert(a); } void sendVector(vector<int> t) { int who = 0; PutInt(who, SIZE(t)); FOREACH (i, t) PutInt(who, *i); Send(who); } vector<int> genMothers() { vector<int> r; r.pb(0); special.insert(0); mothers.insert(0); REP (i, nodes - 1) { int a; do { a = rand() % NumberOfStudents(); } while (special.find(a) != special.end()); r.pb(a); special.insert(a); mothers.insert(a); } return r; } // solver map<int, vector<vector<int> > > adj; map<int, int> pos; void solve() { for (int i = 0; i < nodes; i++) REP (l, 2) { Receive(i); int cnt = GetInt(i); vector<int> path; REP (j, cnt) { path.pb(GetInt(i)); } if (adj.find(path.front()) == adj.end()) adj[path.front()] = vector<vector<int> >(); adj[path.front()].pb(path); } int a = 0; int prev = adj[0][0].back(); pos[a] = 0; do { int x = 0; assert(adj[a].size() == 2); if (adj[a][x].back() == prev) x = 1; for (int i = 0; i < SIZE(adj[a][x]) - 1; i += 2) { int visited = adj[a][x][i]; int w = adj[a][x][i + 1]; pos[adj[a][x][i + 2]] = pos[visited] + w; } int nx = adj[a][x].back(); prev = a; a = nx; } while (a); for (int i = 1; i <= NumberOfQueries(); i++) { int a = QueryFrom(i) - 1; int b = QueryTo(i) - 1; int n = NumberOfStudents(); a = pos[a]; b = pos[b]; int r = min((n + a - b) % n, (n + b - a) % n); cout << r << endl; } } int main() { srand(15); nodes = min(NumberOfNodes(), NumberOfStudents()); vector<int> moth = genMothers(); int ax = 0; FOREACH (i, moth) { if (ax++ == MyNodeId()) mother = *i; } for (int i = 1; i <= NumberOfQueries(); i++) { addQueried(QueryFrom(i) - 1); addQueried(QueryTo(i) - 1); } sendVector(go(mother, firstNeighbor(mother))); sendVector(go(mother, secondNeighbor(mother))); if (MyNodeId() == 0) solve(); 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 152 153 154 155 156 157 158 159 160 | #include "kollib.h" #include "message.h" // Grzegorz Guspiel #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<int(n);++i) #define SIZE(c) ((int)((c).size())) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second template<typename T> void maxE(T& a, const T& b) { a = max(a, b); } template<typename T> void minE(T& a, const T& b) { a = min(a, b); } template<typename T> ostream& operator<<(ostream& out, const vector<T>& t) { out << "["; FOREACH (i, t) out << *i << ", "; out << "]"; return out; } typedef pair<int, int> PII; const int INF = 1000 * 1000 * 1000 + 1000; ofstream out; int nodes; int mother; unordered_set<int> special, mothers, queried; int firstNeighbor(int a) { return FirstNeighbor(a + 1) - 1; } int secondNeighbor(int a) { return SecondNeighbor(a + 1) - 1; } vector<int> go(int a, int b) { int steps = 0; bool isMother; vector<int> r; r.pb(a); do { steps++; bool isSpecial = special.find(b) != special.end(); isMother = 0; if (isSpecial) { isMother = (mothers.find(b) != mothers.end()); if (!isMother && queried.find(b) != queried.end()) { r.pb(steps); steps = 0; r.pb(b); } } int c = firstNeighbor(b); if (c == a) c = secondNeighbor(b); a = b; b = c; } while (!isMother); r.pb(steps); r.pb(a); return r; } void addQueried(int a) { assert(a < NumberOfStudents()); if (queried.find(a) == queried.end()) queried.insert(a); if (special.find(a) == special.end()) special.insert(a); } void sendVector(vector<int> t) { int who = 0; PutInt(who, SIZE(t)); FOREACH (i, t) PutInt(who, *i); Send(who); } vector<int> genMothers() { vector<int> r; r.pb(0); special.insert(0); mothers.insert(0); REP (i, nodes - 1) { int a; do { a = rand() % NumberOfStudents(); } while (special.find(a) != special.end()); r.pb(a); special.insert(a); mothers.insert(a); } return r; } // solver map<int, vector<vector<int> > > adj; map<int, int> pos; void solve() { for (int i = 0; i < nodes; i++) REP (l, 2) { Receive(i); int cnt = GetInt(i); vector<int> path; REP (j, cnt) { path.pb(GetInt(i)); } if (adj.find(path.front()) == adj.end()) adj[path.front()] = vector<vector<int> >(); adj[path.front()].pb(path); } int a = 0; int prev = adj[0][0].back(); pos[a] = 0; do { int x = 0; assert(adj[a].size() == 2); if (adj[a][x].back() == prev) x = 1; for (int i = 0; i < SIZE(adj[a][x]) - 1; i += 2) { int visited = adj[a][x][i]; int w = adj[a][x][i + 1]; pos[adj[a][x][i + 2]] = pos[visited] + w; } int nx = adj[a][x].back(); prev = a; a = nx; } while (a); for (int i = 1; i <= NumberOfQueries(); i++) { int a = QueryFrom(i) - 1; int b = QueryTo(i) - 1; int n = NumberOfStudents(); a = pos[a]; b = pos[b]; int r = min((n + a - b) % n, (n + b - a) % n); cout << r << endl; } } int main() { srand(15); nodes = min(NumberOfNodes(), NumberOfStudents()); vector<int> moth = genMothers(); int ax = 0; FOREACH (i, moth) { if (ax++ == MyNodeId()) mother = *i; } for (int i = 1; i <= NumberOfQueries(); i++) { addQueried(QueryFrom(i) - 1); addQueried(QueryTo(i) - 1); } sendVector(go(mother, firstNeighbor(mother))); sendVector(go(mother, secondNeighbor(mother))); if (MyNodeId() == 0) solve(); return 0; } |