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
#include "sabotaz.h"
#include "message.h"
#include <iostream>
#include <list>
using namespace std;


class Miasto
{
public:

	bool odwiedzoned;
	bool fixed;

	std::list<Miasto*> sasiedzi;

	Miasto()
	{
		odwiedzoned = 0;
		fixed = 0;
	}

	void addSasiad(Miasto* m)
	{
		sasiedzi.push_front(m);
	}
	
	void removeSasiad(Miasto* m)
	{
        for (auto it=sasiedzi.begin(); it!=sasiedzi.end(); ++it)
        {
            if ((*it) == m)
            {
		       sasiedzi.erase(it);
		       return;
            }
         }       
	}
	
	bool tryToGetTo(Miasto* m)
	{
		odwiedzoned = true;
		fixed = false;
		for (auto it=sasiedzi.begin(); it!=sasiedzi.end(); ++it)
        {   
            if ((*it) == m) return true;
            
			if (!(*it)->odwiedzoned)
				if ((*it)->tryToGetTo(m))
					return true;  
		}   
		return false;
    }
	
	bool fixOdwiedzoned(Miasto* m)
	{
		odwiedzoned = false;
		fixed = true;
		for (auto it=sasiedzi.begin(); it!=sasiedzi.end(); ++it)
		{   
            if ((*it) == m) return true;
            
			if (!(*it)->fixed)
				if ((*it)->fixOdwiedzoned(m))
					return true;
		} 
	return false;
	}
};

Miasto** miasta;


int getResult(long long starting, long long ending)
{
    int result = 0;
    
    for(int k=starting; k < ending; k++)
    {
		int a,b;
          a=BridgeEndA(k);
          b=BridgeEndB(k);
          
          if (a==b) continue;
     
          miasta[a]->removeSasiad(miasta[b]);
          miasta[b]->removeSasiad(miasta[a]);

          if (!miasta[a]->tryToGetTo(miasta[b]))
             result++;
          miasta[a]->fixOdwiedzoned(miasta[b]);
     
          miasta[a]->addSasiad(miasta[b]);
          miasta[b]->addSasiad(miasta[a]);  
    }
    
    return result;
}


int main()
{
    std::ios_base::sync_with_stdio(false);
	int n,m;
	n = NumberOfIsles();
	m = NumberOfBridges();
  
	miasta = new Miasto*[n];
	for(int k=0; k<n;k++)
	{
		miasta[k] = new Miasto();
	}
	
	int a,b;
	for(int k=0; k< m ; k++)
	{
		 a=BridgeEndA(k);
		 b=BridgeEndB(k);
     
		miasta[a]->addSasiad(miasta[b]);
		miasta[b]->addSasiad(miasta[a]);
	}
	
	int nodes = NumberOfNodes();
	int instance = MyNodeId();
	
	long long attempts = m;
	long long amount = attempts/nodes;
	long long starting = amount*instance;

	if (instance<attempts%nodes)
	{
		starting+= instance;
	}
	else
	{
		starting += attempts%nodes;
	}

	long long ending = starting + amount;


	if (instance<attempts%nodes)
	{
		ending++;
	}

    int counter = 0;
    if ( starting != ending) counter = getResult(starting, ending);
  
    //cout<<counter<<endl;

    if(instance != 0)
    {
		PutInt(0, counter);
		Send(0);
	}
	else
	{
		long long counted = counter;
		
		for(int k = 1; k<nodes; k++)
		{
			Receive(k);
			int received;
			received = GetInt(k);
			
			//cout<<received<<endl;
			counted += received;
		}
		
		cout<<counted<<endl;
		return 0;
	}

}