#include "kollib.h" #include "message.h" #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define PB(x) push_back(x) #define Si size() #define be begin() #define en end() struct el{ int st, nd; el(int a, int b) : st(a), nd(b) {} }; inline bool operator<(const el& a, const el& b){ return (a.st<b.st); } int main() { int id=MyNodeId(); int N=NumberOfNodes(); int n=NumberOfStudents(); int m=NumberOfQueries(); vector<el> res; for(int i=id+1;i<=m;i+=N){ int u, v, pre1, pre2, w1, w2, tmp, d=1; u=QueryFrom(i); v=QueryTo(i); pre1=pre2=u; if(u==v) res.PB(el(i, 0)); else{ w1=FirstNeighbor(u); w2=SecondNeighbor(u); while(w1!=v && w2!=v){ tmp=w1; if(FirstNeighbor(w1)!=pre1) w1=FirstNeighbor(w1); else w1=SecondNeighbor(w1); pre1=tmp; tmp=w2; if(FirstNeighbor(w2)!=pre2) w2=FirstNeighbor(w2); else w2=SecondNeighbor(w2); pre2=tmp; ++d; } res.PB(el(i, d)); } } if(id>0){ PutInt(0, res.Si); for(int i=0;i<res.Si;++i){ PutInt(0, res[i].st); PutInt(0, res[i].nd); } Send(0); }else{ for(int i=0;i<N;++i){ int from=Receive(-1); int s=GetInt(from); for(int j=0;j<s;++j){ int a=GetInt(from); int b=GetInt(from); res.PB(el(a, b)); } } sort(res.be, res.en); for(int i=0;i<res.Si;++i) printf("%d\n", res[i].nd); } }
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 | #include "kollib.h" #include "message.h" #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define PB(x) push_back(x) #define Si size() #define be begin() #define en end() struct el{ int st, nd; el(int a, int b) : st(a), nd(b) {} }; inline bool operator<(const el& a, const el& b){ return (a.st<b.st); } int main() { int id=MyNodeId(); int N=NumberOfNodes(); int n=NumberOfStudents(); int m=NumberOfQueries(); vector<el> res; for(int i=id+1;i<=m;i+=N){ int u, v, pre1, pre2, w1, w2, tmp, d=1; u=QueryFrom(i); v=QueryTo(i); pre1=pre2=u; if(u==v) res.PB(el(i, 0)); else{ w1=FirstNeighbor(u); w2=SecondNeighbor(u); while(w1!=v && w2!=v){ tmp=w1; if(FirstNeighbor(w1)!=pre1) w1=FirstNeighbor(w1); else w1=SecondNeighbor(w1); pre1=tmp; tmp=w2; if(FirstNeighbor(w2)!=pre2) w2=FirstNeighbor(w2); else w2=SecondNeighbor(w2); pre2=tmp; ++d; } res.PB(el(i, d)); } } if(id>0){ PutInt(0, res.Si); for(int i=0;i<res.Si;++i){ PutInt(0, res[i].st); PutInt(0, res[i].nd); } Send(0); }else{ for(int i=0;i<N;++i){ int from=Receive(-1); int s=GetInt(from); for(int j=0;j<s;++j){ int a=GetInt(from); int b=GetInt(from); res.PB(el(a, b)); } } sort(res.be, res.en); for(int i=0;i<res.Si;++i) printf("%d\n", res[i].nd); } } |