#include "kollib.h" #include "message.h" #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<queue> #include<stack> #include<cmath> #include<map> #include<unordered_set> using namespace std; typedef pair<int,int> PI; typedef long long LL; typedef double D; #define FI first #define SE second #define MP make_pair #define PB push_back #define R(I,N) for(int I=0;I<N;I++) #define F(I,A,B) for(int I=A;I<B;I++) #define FD(I,N) for(int I=N-1;I>=0;I--) #define make(A) scanf("%d",&A) #define MAX 201 int n,m,proc,id,a[MAX],b[MAX]; unordered_set<int> all,s,q; vector<PI> tosend; vector<int> wyr; int rozpatrz(int ak,int il){ R(i,m){ if(a[i] == ak)tosend.PB(MP(i+1,il)); if(b[i] == ak)tosend.PB(MP(-i-1,il)); } } void go(int ak, int pop, bool str){ int kt = 1; while(1){ if(all.count(ak)){ if(s.count(ak))break; if(q.count(ak))rozpatrz(ak,kt*(str?1:-1)); } pop = FirstNeighbor(ak) + SecondNeighbor(ak) - pop; swap(pop,ak); kt++; } PutInt(0,ak); PutInt(0,kt); } void wyslij(){ PutInt(0,tosend.size()); R(i,tosend.size()){ PutInt(0,tosend[i].FI); PutInt(0,tosend[i].SE); } Send(0); } int licz(int nr){ if(q.count(nr))rozpatrz(nr,0); go(FirstNeighbor(nr),nr,1); go(SecondNeighbor(nr),nr,0); wyslij(); } int szu(int nr){ R(i,wyr.size())if(wyr[i] == nr)return i; } int pop[MAX],nas[MAX]; int iln[MAX],ilp[MAX]; int kt[MAX]; bool odw[MAX]; int g[MAX][2]; void lacz(){ R(i,proc){ Receive(i); pop[i] = szu(GetInt(i)); ilp[i] = GetInt(i); nas[i] = szu(GetInt(i)); iln[i] = GetInt(i); } int ak = 0; int sum = 0; int oj = -1; do{ kt[ak] = sum; int pom; if(nas[ak] == oj){ odw[ak] = 1; pom = pop[ak]; sum += ilp[ak]; }else{ pom = nas[ak]; sum += iln[ak]; } oj = ak; ak = pom; }while(ak != 0); R(i,proc){ int pom = GetInt(i); while(pom--){ int zap = GetInt(i); int gdzie = GetInt(i); g[abs(zap)-1][zap > 0] = kt[i] + gdzie * (odw[i]?1:-1); } } R(i,m){ R(j,2){ while(g[i][j]<0)g[i][j]+=n; while(g[i][j]>=n)g[i][j]-=n; } int pom = abs(g[i][0]-g[i][1]); printf("%d\n",min(pom,n-pom)); } } main(){ proc = NumberOfNodes(); id = MyNodeId(); n = NumberOfStudents(); m = NumberOfQueries(); R(i,m){ a[i] = QueryFrom(i+1); b[i] = QueryTo(i+1); q.insert(a[i]); q.insert(b[i]); all.insert(a[i]); all.insert(b[i]); } if(n < proc){ proc = n; if(id >= n) return 0; } srand(41268328); while(s.size() != proc){ int pom = rand()%n+1; all.insert(pom); if(s.insert(pom).SE) wyr.PB(pom); } licz(wyr[id]); if(id == 0)lacz(); }
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 | #include "kollib.h" #include "message.h" #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<queue> #include<stack> #include<cmath> #include<map> #include<unordered_set> using namespace std; typedef pair<int,int> PI; typedef long long LL; typedef double D; #define FI first #define SE second #define MP make_pair #define PB push_back #define R(I,N) for(int I=0;I<N;I++) #define F(I,A,B) for(int I=A;I<B;I++) #define FD(I,N) for(int I=N-1;I>=0;I--) #define make(A) scanf("%d",&A) #define MAX 201 int n,m,proc,id,a[MAX],b[MAX]; unordered_set<int> all,s,q; vector<PI> tosend; vector<int> wyr; int rozpatrz(int ak,int il){ R(i,m){ if(a[i] == ak)tosend.PB(MP(i+1,il)); if(b[i] == ak)tosend.PB(MP(-i-1,il)); } } void go(int ak, int pop, bool str){ int kt = 1; while(1){ if(all.count(ak)){ if(s.count(ak))break; if(q.count(ak))rozpatrz(ak,kt*(str?1:-1)); } pop = FirstNeighbor(ak) + SecondNeighbor(ak) - pop; swap(pop,ak); kt++; } PutInt(0,ak); PutInt(0,kt); } void wyslij(){ PutInt(0,tosend.size()); R(i,tosend.size()){ PutInt(0,tosend[i].FI); PutInt(0,tosend[i].SE); } Send(0); } int licz(int nr){ if(q.count(nr))rozpatrz(nr,0); go(FirstNeighbor(nr),nr,1); go(SecondNeighbor(nr),nr,0); wyslij(); } int szu(int nr){ R(i,wyr.size())if(wyr[i] == nr)return i; } int pop[MAX],nas[MAX]; int iln[MAX],ilp[MAX]; int kt[MAX]; bool odw[MAX]; int g[MAX][2]; void lacz(){ R(i,proc){ Receive(i); pop[i] = szu(GetInt(i)); ilp[i] = GetInt(i); nas[i] = szu(GetInt(i)); iln[i] = GetInt(i); } int ak = 0; int sum = 0; int oj = -1; do{ kt[ak] = sum; int pom; if(nas[ak] == oj){ odw[ak] = 1; pom = pop[ak]; sum += ilp[ak]; }else{ pom = nas[ak]; sum += iln[ak]; } oj = ak; ak = pom; }while(ak != 0); R(i,proc){ int pom = GetInt(i); while(pom--){ int zap = GetInt(i); int gdzie = GetInt(i); g[abs(zap)-1][zap > 0] = kt[i] + gdzie * (odw[i]?1:-1); } } R(i,m){ R(j,2){ while(g[i][j]<0)g[i][j]+=n; while(g[i][j]>=n)g[i][j]-=n; } int pom = abs(g[i][0]-g[i][1]); printf("%d\n",min(pom,n-pom)); } } main(){ proc = NumberOfNodes(); id = MyNodeId(); n = NumberOfStudents(); m = NumberOfQueries(); R(i,m){ a[i] = QueryFrom(i+1); b[i] = QueryTo(i+1); q.insert(a[i]); q.insert(b[i]); all.insert(a[i]); all.insert(b[i]); } if(n < proc){ proc = n; if(id >= n) return 0; } srand(41268328); while(s.size() != proc){ int pom = rand()%n+1; all.insert(pom); if(s.insert(pom).SE) wyr.PB(pom); } licz(wyr[id]); if(id == 0)lacz(); } |