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
//Przemysław Jakub Kozłowski
#include "sabotaz.h"
#include "message.h"
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define FI first
#define SE second
#define MP make_pair
using namespace std;
const int MAXN = 200005;

int NUM, ID;
int n,m;

inline pair<int,int> getprz(int fid)
{
    int fpodz = m/NUM;
    return MP(fpodz*fid+1, fid == NUM-1 ? m : fpodz*fid+fpodz);
}

void FUInit(vector<int> &FU)
{
    FU.resize(n+1);
    for(int i = 1;i <= n;i++) FU[i] = i;
}
vector<int> FUstos;
int FUFind(vector<int> &FU, int x)
{
    while(x != FU[x])
    {
        FUstos.push_back(x);
        x = FU[x];
    }
    while(!FUstos.empty())
    {
        FU[FUstos.back()] = x;
        FUstos.pop_back();
    }
    return x;
}
void FUUnion(vector<int> &FU, int a, int b)
{
    a = FUFind(FU, a);
    b = FUFind(FU, b);
    if(a > b) swap(a,b);
    FU[b] = a;
}
void FUclean(vector<int> &FU)
{
    for(int i = 1;i <= n;i++)
        FU[i] = FU[FU[i]];
}
void FUappend(vector<int> &FUA, vector<int> &FUB)
{
    FUclean(FUB);
    for(int i = 1;i <= n;i++)
    {
        //int x = FUFind(FUA, i);
        //int y = FUFind(FUA, FUB[i]);
        //FUA[i] = min(x, y);
        FUUnion(FUA, i, FUB[i]);
    }
}
/*void FUPut(int target, const vector<int> &FU)
{
    for(int i = 1;i <= n;i++)
        PutInt(target, FU[i]);
}
vector<int> FUGet(int source)
{
    vector<int> ret(n+1);
    for(int i = 1;i <= n;i++)
        ret[i] = GetInt(source);
    return ret;
}*/
const int PODZIAL = 2000;
void FUSend(int target, const vector<int> &FU)
{
    for(int nr = 1;nr <= (n+PODZIAL-1)/PODZIAL;nr++)
    {
        for(int i = (nr-1)*PODZIAL+1;i <= min(nr*PODZIAL, n);i++)
            PutInt(target, FU[i]);
        Send(target);
    }
}
vector<int> FUReceive(int source)
{
    vector<int> ret(n+1);
    for(int nr = 1;nr <= (n+PODZIAL-1)/PODZIAL;nr++)
    {
        Receive(source);
        for(int i = (nr-1)*PODZIAL+1;i <= min(nr*PODZIAL, n);i++)
            ret[i] = GetInt(source);
    }
    return ret;
}

void skocz(vector<int> &FUter, int skok)
{
    FUSend((ID-skok+NUM)%NUM, FUter);
    vector<int> FUtmp = FUReceive((ID+skok)%NUM);
    FUappend(FUter, FUtmp);
}

vector<int> V[MAXN];
int czas = 1;
int num[MAXN];
int ojc[MAXN];
int low[MAXN];

void DFS(int x)
{
    char byl_ojciec = false;
    num[x] = ++czas;
    low[x] = num[x];

    for(int i = 0;i < V[x].size();i++)
    {
        if(V[x][i] == ojc[x] && !byl_ojciec) byl_ojciec = true;
        else if(!ojc[V[x][i]])
        {
            ojc[V[x][i]] = x;
            DFS(V[x][i]);
            low[x] = min(low[x], low[V[x][i]]);
        }
        else
        {
            low[x] = min(low[x], num[V[x][i]]);
        }
    }
}

int main()
{
    NUM = NumberOfNodes();
    ID = MyNodeId();
    n = NumberOfIsles();
    m = NumberOfBridges();

    if(NUM > m)
    {
        NUM = m;
    }
    if(m <= 5000000) NUM = 1;
    if(ID >= NUM) return 0;

    vector<int> FUter;
    FUInit(FUter);

    pair<int,int> prz = getprz(ID);
    for(int i = prz.FI;i <= prz.SE;i++)
    {
        int a = BridgeEndA(i-1)+1;
        int b = BridgeEndB(i-1)+1;
        if(a != b)
        {
            V[a].push_back(b);
            V[b].push_back(a);
            FUUnion(FUter, a,b);
        }
    }

    int logNUM = 0;
    while((1<<(logNUM+1)) <= NUM-1) logNUM++;

    for(int i = 1;i <= logNUM;i++)
        skocz(FUter, (1<<(i-1)));
    skocz(FUter, (NUM-1)-(1<<logNUM));

    FUSend((ID-1+NUM)%NUM, FUter);
    FUter = FUReceive((ID+1+NUM)%NUM);

    FUclean(FUter);

    // Następna faza - mamy FUter
    for(int i = 1;i <= n;i++)
        if(!low[i])
        {
            ojc[i] = -1;
            DFS(i);
        }

    int twyn = 0;
    for(int i = 1;i <= n;i++)
        if(ojc[i] != -1 && low[i] == num[i] && (NUM == 1 || FUter[i] != FUter[ojc[i]]))
                twyn++;

    PutInt(0, twyn);
    Send(0);

    if(ID == 0)
    {
        int wyn = 0;
        for(int j = 0;j < NUM;j++)
        {
            Receive(j);
            wyn += GetInt(j);
        }
        printf("%d\n", wyn);
    }


    return 0;
}