#include<cstdio> #include<algorithm> #include<cassert> #include<complex> #include<map> #include<iomanip> #include<sstream> #include<queue> #include<set> #include<string> #include<vector> #include<iostream> #include<cstring> #include<stack> #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 fup FOR #define fdo FORD #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define siz SZ #define CLR(x) memset((x), 0, sizeof(x)) #define PB push_back #define MP make_pair #define X first #define Y second #define FI X #define SE Y #define SQR(a) ((a)*(a)) #define DEBUG 1 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FORE(it, (x)) cerr <<*it <<", "; cout <<endl; }} using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int>P; typedef vector<int>VI; const int INF=1E9+7; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } #include "message.h" #include "kollib.h" const int MAX_R = 10000; const int MAX = 200*2+MAX_R; set<int> s; map<int,int> Whereis; struct W {int a; int index[2]; int dist[2];}; W w[MAX+7]; // 0==NULL ; indexowana od 0 int w_size=0; int my, c, n, m; void compute(int i) { int last = w[i].a; int x[2] = {FirstNeighbor(last), SecondNeighbor(last)}; FOR(z,0,1) { int last = w[i].a; int dist = 1; int next = x[z]; while (s.find(next)==s.end()) { int f = FirstNeighbor(next); int s = SecondNeighbor(next); if (f==last) { last = next; next = s; } else { last = next; next = f; } dist++; } w[i].index[z]=Whereis[next]; w[i].dist[z]=dist; } } int calc_dist(int i, int j) { if (i==j) return 0; int pocz_i = Whereis[i]; int last_i = pocz_i; int next_i = w[pocz_i].index[0]; int dist = w[pocz_i].dist[0]; while (w[next_i].a!=j) { int choice = (w[next_i].index[0]==last_i)?1:0; last_i = next_i; dist += w[next_i].dist[choice]; next_i = w[next_i].index[choice]; } return min(dist, n-dist); } /*int NumberOfNodes() { return 1;} int MyNodeId() { return 0;} void PutInt(int,int){} void Send(int){} int Receive(int){} int GetInt(int){} */ int main(){ ios_base::sync_with_stdio(false); cout << setprecision(15) << fixed; n = NumberOfStudents(); m = NumberOfQueries(); c = NumberOfNodes(); my = MyNodeId(); FOR(i,1,m) s.insert(QueryFrom(i)); FOR(i,1,m) s.insert(QueryTo(i)); srand(2); FOR(i,1,MAX_R) s.insert((rand()%n)+1); FORE(i,s) {Whereis[*i]=w_size; w[w_size++].a = *i;} while(my<w_size) { compute(my); if (MyNodeId()>0) { PutInt(0,my); PutInt(0,w[my].index[0]); PutInt(0,w[my].index[1]); PutInt(0,w[my].dist[0]); PutInt(0,w[my].dist[1]); } my+=c; } if (MyNodeId()>0) { PutInt(0,INF); Send(0); } else { FOR(i,1,c-1) { Receive(i); while(1) { my = GetInt(i); if (my==INF) break; w[my].index[0] = GetInt(i); w[my].index[1] = GetInt(i); w[my].dist[0] = GetInt(i); w[my].dist[1] = GetInt(i); } } FOR(i,1,m) cout<<calc_dist(QueryFrom(i), QueryTo(i))<<endl; } 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 | #include<cstdio> #include<algorithm> #include<cassert> #include<complex> #include<map> #include<iomanip> #include<sstream> #include<queue> #include<set> #include<string> #include<vector> #include<iostream> #include<cstring> #include<stack> #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 fup FOR #define fdo FORD #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define siz SZ #define CLR(x) memset((x), 0, sizeof(x)) #define PB push_back #define MP make_pair #define X first #define Y second #define FI X #define SE Y #define SQR(a) ((a)*(a)) #define DEBUG 1 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FORE(it, (x)) cerr <<*it <<", "; cout <<endl; }} using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int>P; typedef vector<int>VI; const int INF=1E9+7; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } #include "message.h" #include "kollib.h" const int MAX_R = 10000; const int MAX = 200*2+MAX_R; set<int> s; map<int,int> Whereis; struct W {int a; int index[2]; int dist[2];}; W w[MAX+7]; // 0==NULL ; indexowana od 0 int w_size=0; int my, c, n, m; void compute(int i) { int last = w[i].a; int x[2] = {FirstNeighbor(last), SecondNeighbor(last)}; FOR(z,0,1) { int last = w[i].a; int dist = 1; int next = x[z]; while (s.find(next)==s.end()) { int f = FirstNeighbor(next); int s = SecondNeighbor(next); if (f==last) { last = next; next = s; } else { last = next; next = f; } dist++; } w[i].index[z]=Whereis[next]; w[i].dist[z]=dist; } } int calc_dist(int i, int j) { if (i==j) return 0; int pocz_i = Whereis[i]; int last_i = pocz_i; int next_i = w[pocz_i].index[0]; int dist = w[pocz_i].dist[0]; while (w[next_i].a!=j) { int choice = (w[next_i].index[0]==last_i)?1:0; last_i = next_i; dist += w[next_i].dist[choice]; next_i = w[next_i].index[choice]; } return min(dist, n-dist); } /*int NumberOfNodes() { return 1;} int MyNodeId() { return 0;} void PutInt(int,int){} void Send(int){} int Receive(int){} int GetInt(int){} */ int main(){ ios_base::sync_with_stdio(false); cout << setprecision(15) << fixed; n = NumberOfStudents(); m = NumberOfQueries(); c = NumberOfNodes(); my = MyNodeId(); FOR(i,1,m) s.insert(QueryFrom(i)); FOR(i,1,m) s.insert(QueryTo(i)); srand(2); FOR(i,1,MAX_R) s.insert((rand()%n)+1); FORE(i,s) {Whereis[*i]=w_size; w[w_size++].a = *i;} while(my<w_size) { compute(my); if (MyNodeId()>0) { PutInt(0,my); PutInt(0,w[my].index[0]); PutInt(0,w[my].index[1]); PutInt(0,w[my].dist[0]); PutInt(0,w[my].dist[1]); } my+=c; } if (MyNodeId()>0) { PutInt(0,INF); Send(0); } else { FOR(i,1,c-1) { Receive(i); while(1) { my = GetInt(i); if (my==INF) break; w[my].index[0] = GetInt(i); w[my].index[1] = GetInt(i); w[my].dist[0] = GetInt(i); w[my].dist[1] = GetInt(i); } } FOR(i,1,m) cout<<calc_dist(QueryFrom(i), QueryTo(i))<<endl; } return 0; } |