#include <cstdio> #include <iostream> #include <sstream> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <set> #include <map> #include <bitset> #include <algorithm> #include <iterator> #include <cstring> #include <string> #include <cmath> #include <numeric> #include <ctype.h> #include <cctype> #include <complex> #include <cassert> #include <cstdlib> #include <functional> #include <utility> #include <ctime> #include "kollib.h" #include "message.h" //#include <ext/hash_set> //#include <ext/hash_map> //struct hasher{ size_t operator()(const string& s) const{ // return hash<const char*>()(s.c_str());} }; using namespace std; using namespace __gnu_cxx; #define PB push_back #define PF push_front #define MP make_pair #define X first #define Y second #define ST first #define ND second #define MT(x,y,z) MP(MP(x,y),z) #define XT X.X #define YT X.Y #define ZT Y #define REP(i,n) for(int i=0;i<(n);i++) #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 FORE(i,x) for(VAR(i,(x).begin());i!=(x).end();++i) #define FORDE(i,x) for(VAR(i,(x).rbegin());i!=(x).rend();++i) #define FOREACH(i,x) FORE(i,x) #define VAR(v,n) __typeof(n) v = (n) #define IT(x) __typeof((x).begin()) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(ALL(c),x) != (c).end()) #define SIZE(x) (int)((x).size()) #define ALL(x) (x).begin(),(x).end() #define CLR(x) memset((x),0,sizeof (x)) #define CPY(to,from) memcpy(to,from,sizeof (from)) #define IND(t,n,v) lower_bound(t,t+n,v) - t #define sqr(a) ((a)*(a)) #define db(x) { if (DEBU) cerr << "line " << __LINE__ << " " << #x \ << " = " << x << "\n"; } #define dbn(x) { if (DEBU) cerr << "\n"; } #define dba(x) { if (DEBU) { cerr << "line " << __LINE__ << " " << #x \ << " = {"; FORE(qwe,x) {if(qwe!=(x).begin()) cerr << ","; \ cerr << *qwe; } cerr << "}\n"; } } #define dbt(t,x) { if (DEBU) { REP(qwe,x) cerr << #t << "[" << qwe << "]=" \ << t[qwe] << " "; cerr << "\n"; }} #define dbtt(t,x,y) { if (DEBU) { REP(qwe,x) { REP(rty,y) cerr << #t \ << "[" << qwe << "][" << rty << "]=" << t[qwe][rty] \ << " "; cerr << "\n"; }}} #define dbb(x,r) { if (DEBU) { bitset<r> qwe(x.to_ulong()); \ cerr << "line " << __LINE__ << " " << #x \ << " = " << qwe << "\n"; } } #define dbbt(t,x,r) { if (DEBU) REP(rty,x) dbb(t[rty],r); } typedef long double ld; typedef unsigned int ui; typedef unsigned long long ull; typedef long long int lli, LL; typedef pair<int, int> pii, PII; typedef pair<pii, int> piii; typedef pair<lli, lli> pll; typedef pair<pll, lli> plll; typedef vector<int> vi, VI; typedef vector<pii> vii; typedef vector<piii> viii; typedef vector<lli> vl; typedef vector<pll> vll; typedef vector<plll> vlll; typedef vector<double> vd; typedef vector<string> vs; typedef vector<vi> vvi; template<class T,class U>ostream & operator<<(ostream&os,const pair<T,U>&qwe){ return os << "(" << qwe.X << "," << qwe.Y << ")"; } template<class T> ostream& operator << (ostream &os, const vector<T>& qwe){ REP(rty,(int)qwe.size()) os << "vec[" << rty << "]=" << qwe[rty] << " "; return os << "\n"; } template<class T> ostream& operator << (ostream &os, const set<T>& qwe){ os << "set:{"; FORE(rty,qwe) {if(rty!=qwe.begin()) os<<","; os << *rty;} return os << "}\n"; } template<class T> ostream& operator << (ostream &os, const list<T>& qwe){ os << "list:{"; FORE(rty,qwe) {if(rty!=qwe.begin()) os<<","; os << *rty;} return os << "}\n"; } const double E = 2.718281828459045235360287471351; const double PI = 3.1415926535897932384626433832795; #define IsZero(x) (x>=-EPS && x<=EPS) #define sgn(x) (IsZero(x)?0:((x<0)?-1:1)) lli los(lli l, lli r) { return l + (lli) ((double)(r-l+1) * (rand() / (RAND_MAX + 1.0))); } string cts(char c) { string s = ""; s+=c; return s; } template<class T> void minX(T &x, T y){ if(x>y)x=y; } template<class T> void maxX(T &x, T y){ if(x<y)x=y; } template<class T> void min(T x, T y, T z){ return min(min(x,y),z); } template<class T> void max(T x, T y, T z){ return max(max(x,y),z); } template <class Ty, class Tx> Ty cast(const Tx &x) { Ty y; stringstream ss(""); ss<<x; ss.seekg(0); ss>>y; return y; } int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;} // #pragma comment (linker, "/STACK:256000000") // #x zwraca nazwe zmiennej a ## b laczy napisy i daje zmienna ab // iostream:cin,getline(cin,string) // stdio:scanf,gets(char*),fgets(char*),getchar() // fgets nie wczytuje bialych znakow z nastepnej linii, dla stringa iostream // iostream: cout; stdio: putchar(char) printf // ios_base::sync_with_stdio(0); przyspiesza iostreama, nie mieszac z stdio // freopen ("in.txt","r",stdin); freopen ("out.txt","w",stdout); ... // fclose (stdin); fclose (stdout); // while(scanf("%d",&n)!=EOF) '\n' == 10 // %c %s %d %u %hd short int %lld %f float %lf double %Lf long double // %x unsigned hex small %X unsigned hex big %o octal %llu unsigned lli // (%5d, 2) 2 (%05d,2)00002 (%x,100) 64 (%#x,100) 0x64 (%#o,100) 0144 // (%6.2lf, 2) 2.00 (%+d,5) +5 (%*d,5,10) 10 (%.*f,3,2) 2.000 (0*d,3,1) 003 // // int -> char* : int a; char b[N]; sprintf(b, "%d", a); // char* -> int : int a; char b[N]; sscanf(b, "%d", &a); // int -> char* podstawa base : char a[N]; itoa(int, a, base) // char* -> int podstawa base : char a[N]; strtol(a, NULL, base) // atoi(char*) to int, atof(char*) to double // string s; stringstream ss(s); while(ss >> buf) res.PB(buf) // // string -> char* : string a; a.c_str(); // char* -> string : char a[N]; string(a); // porowananie char* : strcmp(char*,char*) np return strcmp(&c[a],&c[b])<0; // strlen strcpy strncpy strdup strcat strcmp strncmp // strchr strcspn strpbrk strstr strrev // isupper(int) islower(int) toupper(int) tolower(int) // isspace isalpha isalnum isdigit isxdigit ispunct // // sort(ALL(v),less<int>()) rosnaca, sort((v),greater<int>()) malejaca // bool func(pii a,pii b){return a.X*a.X+a.Y*a.Y < b.X*b.X+b.Y*b.Y;} // struct T {string name; lli X,Y;}; // bool operator<(T a,T b){return a.X*a.X+b.Y*b.Y < a.X*a.X+b.Y*b.Y;} // // bitset<N> bs(lli); // set() filp() reset() size() count() any() none() [] to_ulong() to_string() // __builtin_popcount(int) lli nie dziala, __ gcd(int,int), x<<32 does nothing // __builtin_clz(int) __builtin_ctz(int) for 0 undefined // REP(s,(1<<N)) - wszystkie podzbiory for(s=X;s!=0;s=(s-1)&X) - podzbiory X // !x&(x-1) pow 2, x&(-x) x&~(x-1) najmniej znaczac bit, ffs(int) jego indeks // memset(t,0/1/-1,sizeof(t)) 1l 1ll 1u 1f // // set.size() liniowo, set.end() O(1), set.erase(it++), multiset.erase(x) wszystkie x // priority_queue: push,top,pop queue:push,pop,front,back // set/map:insert,erase,find,set.lower_bound list:insert,erase,reverse // deque(jak vector) / list:back,front,push_back,push_front,pop_back,pop_front // hash_set<string,hasher> hs; hash_map<string,int,hasher> hm; hm.bucket_count() // // exit(0) M_PI fabsl abs labs llabs sqrtl llround round(0.5)=1 // lower_bound - pierwszy nie mniejszy upper_bound - pierwszy wiekszy // min_element(All(x)) max_element(ALL(x)) // sort(ALL(v)); v.erase(v.unique(),v.end()) // set_union, set_intersection, set_differenece, set_symmetric_difference // random_shuffle RAND_MAX int rand()=random() srand(int seed) // complex<double> x; real,imag,abs,arg,norm,conj,polar,cos,exp,sin,sqrt // LR :: ++ -- () [] . -> RL ++ -- (unary)+- ! ~ (type) * (address)& // LR .* ->* * / % + - << >> < <= > >= == != & ^ | && || ?: = += -= itd const double EPS = 1e-9; #define INF 1000000005 #define MAXN 100005 #define MAXM 100005 #define DEBU 1 #define N 100005 #define M 100005 #define MOD 123141 pii qr[205]; set<int> s; map<int, vector<pii> > chs[2]; int main(){ int n = NumberOfStudents(); int q = NumberOfQueries(); FOR(i,1,q) { qr[i].X = QueryFrom(i); qr[i].Y = QueryTo(i); s.insert(qr[i].X); s.insert(qr[i].Y); } int nods = NumberOfNodes() - 1; int mynod = MyNodeId(); int partNum = (n-1)/MOD + 1; if(mynod > 0){ for(int i = (mynod - 1) * MOD + 1; i <= n; i += MOD * nods){ for(int j = 0; j <= 1; j++){ int x1 = i; int x2 = (!j?FirstNeighbor(i):SecondNeighbor(i)); vector<pii> res; res.PB(MP(0,i)); int len = 0; while(x2 % MOD != 1) { if(present(s, x2)) { res.PB(MP(len,x2)); len = 0; } else len++; int temp = FirstNeighbor(x2); if(temp == x1) temp = SecondNeighbor(x2); x1 = x2; x2 = temp; } res.PB(MP(len,x2)); PutInt(0, res.size()); //printf("Przesylam %d \n", res.size()); FORE(k,res) { PutInt(0, k->X); PutInt(0, k->Y); } //printf("wysylka %d %d \n", k->X, k->Y); } Send(0); } } return 0 ; } else { int toReceive = 2 * partNum; while(toReceive--) { int src = Receive(-1); //printf("src %d \n", src); int sz = GetInt(src); //printf("sz %d \n", sz); vector<pii> res; REP(i,sz) { int len = GetInt(src); int v = GetInt(src); res.PB(MP(len,v)); } // printf(" odbior %d %d\n", len, v); } int v = res[0].Y; if(!present(chs[0], v)) chs[0][v] = res; else chs[1][v] = res; } } int v = 1, prev = 0, len = 0; vector<pii> chain; REP(i,partNum){ int idx = 0; if(chs[idx][v].back().Y == prev) idx++; vector<pii> akt = chs[idx][v]; //FORE(j, akt) printf(" part chain: %d %d %d\n", i, j->X, j->Y); FOR(i,1,akt.size()-2) { chain.PB(MP(len + akt[i].X, akt[i].Y)); len = 0; } prev = v; v = akt.back().Y; if(present(s, akt.back().Y)) { chain.PB(MP(len + akt.back().X, akt.back().Y)); len = 0; } else len += akt.back().X + 1; } chain[0].X += len; //FORE(j,chain) printf(" chain : %d %d\n", j->X, j->Y); FOR(i,1,q){ if(qr[i].X == qr[i].Y) { printf("0\n"); continue; } bool licz = 0; len = 0; FORE(j,chain) { if(licz) len += j->X + 1; if(j->Y == qr[i].X || j->Y == qr[i].Y) licz = !licz; //printf(" wynik : %d %d %d %d %d %d\n", len, licz, j->X, j->Y, qr[i].X, qr[i].Y); } len = min(len, n - len); printf("%d\n", len); } 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 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | #include <cstdio> #include <iostream> #include <sstream> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <set> #include <map> #include <bitset> #include <algorithm> #include <iterator> #include <cstring> #include <string> #include <cmath> #include <numeric> #include <ctype.h> #include <cctype> #include <complex> #include <cassert> #include <cstdlib> #include <functional> #include <utility> #include <ctime> #include "kollib.h" #include "message.h" //#include <ext/hash_set> //#include <ext/hash_map> //struct hasher{ size_t operator()(const string& s) const{ // return hash<const char*>()(s.c_str());} }; using namespace std; using namespace __gnu_cxx; #define PB push_back #define PF push_front #define MP make_pair #define X first #define Y second #define ST first #define ND second #define MT(x,y,z) MP(MP(x,y),z) #define XT X.X #define YT X.Y #define ZT Y #define REP(i,n) for(int i=0;i<(n);i++) #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 FORE(i,x) for(VAR(i,(x).begin());i!=(x).end();++i) #define FORDE(i,x) for(VAR(i,(x).rbegin());i!=(x).rend();++i) #define FOREACH(i,x) FORE(i,x) #define VAR(v,n) __typeof(n) v = (n) #define IT(x) __typeof((x).begin()) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(ALL(c),x) != (c).end()) #define SIZE(x) (int)((x).size()) #define ALL(x) (x).begin(),(x).end() #define CLR(x) memset((x),0,sizeof (x)) #define CPY(to,from) memcpy(to,from,sizeof (from)) #define IND(t,n,v) lower_bound(t,t+n,v) - t #define sqr(a) ((a)*(a)) #define db(x) { if (DEBU) cerr << "line " << __LINE__ << " " << #x \ << " = " << x << "\n"; } #define dbn(x) { if (DEBU) cerr << "\n"; } #define dba(x) { if (DEBU) { cerr << "line " << __LINE__ << " " << #x \ << " = {"; FORE(qwe,x) {if(qwe!=(x).begin()) cerr << ","; \ cerr << *qwe; } cerr << "}\n"; } } #define dbt(t,x) { if (DEBU) { REP(qwe,x) cerr << #t << "[" << qwe << "]=" \ << t[qwe] << " "; cerr << "\n"; }} #define dbtt(t,x,y) { if (DEBU) { REP(qwe,x) { REP(rty,y) cerr << #t \ << "[" << qwe << "][" << rty << "]=" << t[qwe][rty] \ << " "; cerr << "\n"; }}} #define dbb(x,r) { if (DEBU) { bitset<r> qwe(x.to_ulong()); \ cerr << "line " << __LINE__ << " " << #x \ << " = " << qwe << "\n"; } } #define dbbt(t,x,r) { if (DEBU) REP(rty,x) dbb(t[rty],r); } typedef long double ld; typedef unsigned int ui; typedef unsigned long long ull; typedef long long int lli, LL; typedef pair<int, int> pii, PII; typedef pair<pii, int> piii; typedef pair<lli, lli> pll; typedef pair<pll, lli> plll; typedef vector<int> vi, VI; typedef vector<pii> vii; typedef vector<piii> viii; typedef vector<lli> vl; typedef vector<pll> vll; typedef vector<plll> vlll; typedef vector<double> vd; typedef vector<string> vs; typedef vector<vi> vvi; template<class T,class U>ostream & operator<<(ostream&os,const pair<T,U>&qwe){ return os << "(" << qwe.X << "," << qwe.Y << ")"; } template<class T> ostream& operator << (ostream &os, const vector<T>& qwe){ REP(rty,(int)qwe.size()) os << "vec[" << rty << "]=" << qwe[rty] << " "; return os << "\n"; } template<class T> ostream& operator << (ostream &os, const set<T>& qwe){ os << "set:{"; FORE(rty,qwe) {if(rty!=qwe.begin()) os<<","; os << *rty;} return os << "}\n"; } template<class T> ostream& operator << (ostream &os, const list<T>& qwe){ os << "list:{"; FORE(rty,qwe) {if(rty!=qwe.begin()) os<<","; os << *rty;} return os << "}\n"; } const double E = 2.718281828459045235360287471351; const double PI = 3.1415926535897932384626433832795; #define IsZero(x) (x>=-EPS && x<=EPS) #define sgn(x) (IsZero(x)?0:((x<0)?-1:1)) lli los(lli l, lli r) { return l + (lli) ((double)(r-l+1) * (rand() / (RAND_MAX + 1.0))); } string cts(char c) { string s = ""; s+=c; return s; } template<class T> void minX(T &x, T y){ if(x>y)x=y; } template<class T> void maxX(T &x, T y){ if(x<y)x=y; } template<class T> void min(T x, T y, T z){ return min(min(x,y),z); } template<class T> void max(T x, T y, T z){ return max(max(x,y),z); } template <class Ty, class Tx> Ty cast(const Tx &x) { Ty y; stringstream ss(""); ss<<x; ss.seekg(0); ss>>y; return y; } int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;} // #pragma comment (linker, "/STACK:256000000") // #x zwraca nazwe zmiennej a ## b laczy napisy i daje zmienna ab // iostream:cin,getline(cin,string) // stdio:scanf,gets(char*),fgets(char*),getchar() // fgets nie wczytuje bialych znakow z nastepnej linii, dla stringa iostream // iostream: cout; stdio: putchar(char) printf // ios_base::sync_with_stdio(0); przyspiesza iostreama, nie mieszac z stdio // freopen ("in.txt","r",stdin); freopen ("out.txt","w",stdout); ... // fclose (stdin); fclose (stdout); // while(scanf("%d",&n)!=EOF) '\n' == 10 // %c %s %d %u %hd short int %lld %f float %lf double %Lf long double // %x unsigned hex small %X unsigned hex big %o octal %llu unsigned lli // (%5d, 2) 2 (%05d,2)00002 (%x,100) 64 (%#x,100) 0x64 (%#o,100) 0144 // (%6.2lf, 2) 2.00 (%+d,5) +5 (%*d,5,10) 10 (%.*f,3,2) 2.000 (0*d,3,1) 003 // // int -> char* : int a; char b[N]; sprintf(b, "%d", a); // char* -> int : int a; char b[N]; sscanf(b, "%d", &a); // int -> char* podstawa base : char a[N]; itoa(int, a, base) // char* -> int podstawa base : char a[N]; strtol(a, NULL, base) // atoi(char*) to int, atof(char*) to double // string s; stringstream ss(s); while(ss >> buf) res.PB(buf) // // string -> char* : string a; a.c_str(); // char* -> string : char a[N]; string(a); // porowananie char* : strcmp(char*,char*) np return strcmp(&c[a],&c[b])<0; // strlen strcpy strncpy strdup strcat strcmp strncmp // strchr strcspn strpbrk strstr strrev // isupper(int) islower(int) toupper(int) tolower(int) // isspace isalpha isalnum isdigit isxdigit ispunct // // sort(ALL(v),less<int>()) rosnaca, sort((v),greater<int>()) malejaca // bool func(pii a,pii b){return a.X*a.X+a.Y*a.Y < b.X*b.X+b.Y*b.Y;} // struct T {string name; lli X,Y;}; // bool operator<(T a,T b){return a.X*a.X+b.Y*b.Y < a.X*a.X+b.Y*b.Y;} // // bitset<N> bs(lli); // set() filp() reset() size() count() any() none() [] to_ulong() to_string() // __builtin_popcount(int) lli nie dziala, __ gcd(int,int), x<<32 does nothing // __builtin_clz(int) __builtin_ctz(int) for 0 undefined // REP(s,(1<<N)) - wszystkie podzbiory for(s=X;s!=0;s=(s-1)&X) - podzbiory X // !x&(x-1) pow 2, x&(-x) x&~(x-1) najmniej znaczac bit, ffs(int) jego indeks // memset(t,0/1/-1,sizeof(t)) 1l 1ll 1u 1f // // set.size() liniowo, set.end() O(1), set.erase(it++), multiset.erase(x) wszystkie x // priority_queue: push,top,pop queue:push,pop,front,back // set/map:insert,erase,find,set.lower_bound list:insert,erase,reverse // deque(jak vector) / list:back,front,push_back,push_front,pop_back,pop_front // hash_set<string,hasher> hs; hash_map<string,int,hasher> hm; hm.bucket_count() // // exit(0) M_PI fabsl abs labs llabs sqrtl llround round(0.5)=1 // lower_bound - pierwszy nie mniejszy upper_bound - pierwszy wiekszy // min_element(All(x)) max_element(ALL(x)) // sort(ALL(v)); v.erase(v.unique(),v.end()) // set_union, set_intersection, set_differenece, set_symmetric_difference // random_shuffle RAND_MAX int rand()=random() srand(int seed) // complex<double> x; real,imag,abs,arg,norm,conj,polar,cos,exp,sin,sqrt // LR :: ++ -- () [] . -> RL ++ -- (unary)+- ! ~ (type) * (address)& // LR .* ->* * / % + - << >> < <= > >= == != & ^ | && || ?: = += -= itd const double EPS = 1e-9; #define INF 1000000005 #define MAXN 100005 #define MAXM 100005 #define DEBU 1 #define N 100005 #define M 100005 #define MOD 123141 pii qr[205]; set<int> s; map<int, vector<pii> > chs[2]; int main(){ int n = NumberOfStudents(); int q = NumberOfQueries(); FOR(i,1,q) { qr[i].X = QueryFrom(i); qr[i].Y = QueryTo(i); s.insert(qr[i].X); s.insert(qr[i].Y); } int nods = NumberOfNodes() - 1; int mynod = MyNodeId(); int partNum = (n-1)/MOD + 1; if(mynod > 0){ for(int i = (mynod - 1) * MOD + 1; i <= n; i += MOD * nods){ for(int j = 0; j <= 1; j++){ int x1 = i; int x2 = (!j?FirstNeighbor(i):SecondNeighbor(i)); vector<pii> res; res.PB(MP(0,i)); int len = 0; while(x2 % MOD != 1) { if(present(s, x2)) { res.PB(MP(len,x2)); len = 0; } else len++; int temp = FirstNeighbor(x2); if(temp == x1) temp = SecondNeighbor(x2); x1 = x2; x2 = temp; } res.PB(MP(len,x2)); PutInt(0, res.size()); //printf("Przesylam %d \n", res.size()); FORE(k,res) { PutInt(0, k->X); PutInt(0, k->Y); } //printf("wysylka %d %d \n", k->X, k->Y); } Send(0); } } return 0 ; } else { int toReceive = 2 * partNum; while(toReceive--) { int src = Receive(-1); //printf("src %d \n", src); int sz = GetInt(src); //printf("sz %d \n", sz); vector<pii> res; REP(i,sz) { int len = GetInt(src); int v = GetInt(src); res.PB(MP(len,v)); } // printf(" odbior %d %d\n", len, v); } int v = res[0].Y; if(!present(chs[0], v)) chs[0][v] = res; else chs[1][v] = res; } } int v = 1, prev = 0, len = 0; vector<pii> chain; REP(i,partNum){ int idx = 0; if(chs[idx][v].back().Y == prev) idx++; vector<pii> akt = chs[idx][v]; //FORE(j, akt) printf(" part chain: %d %d %d\n", i, j->X, j->Y); FOR(i,1,akt.size()-2) { chain.PB(MP(len + akt[i].X, akt[i].Y)); len = 0; } prev = v; v = akt.back().Y; if(present(s, akt.back().Y)) { chain.PB(MP(len + akt.back().X, akt.back().Y)); len = 0; } else len += akt.back().X + 1; } chain[0].X += len; //FORE(j,chain) printf(" chain : %d %d\n", j->X, j->Y); FOR(i,1,q){ if(qr[i].X == qr[i].Y) { printf("0\n"); continue; } bool licz = 0; len = 0; FORE(j,chain) { if(licz) len += j->X + 1; if(j->Y == qr[i].X || j->Y == qr[i].Y) licz = !licz; //printf(" wynik : %d %d %d %d %d %d\n", len, licz, j->X, j->Y, qr[i].X, qr[i].Y); } len = min(len, n - len); printf("%d\n", len); } return 0; } |