# E. Egor in the Republic of Dagestan SOLUTIONS Codeforces Round #669 (Div. 2)

## Egor in the Republic of Dagestan SOLUTION

Egor is an acclaimed Russian vocalist, rapper, entertainer and blogger, lastly he chose to give a show in the bright Republic of Dagestan.
There are n urban communities in the republic, some of them are associated by m coordinated streets with no extra conditions. As it were, street arrangement of Dagestan speaks to a discretionary coordinated chart. Egor will show up to the city 1, travel to the city n by streets along some way, part with a show and fly.
As any celebrated craftsman, Egor has bunches of haters and too irritating fans, so he can travel just by safe streets. There are two kinds of the streets in Dagestan, highly contrasting: dark streets are sheltered around evening time just, and white streets — in the first part of the day. Before the excursion Egor’s director’s going to make a timetable: for every city he’ll indicate it’s shading, dark or white, and afterward if during the outing they visit some city, the main time they can leave it is controlled by the city’s shading: night, if it’s dark, and morning, if it’s white. Subsequent to making the timetable Egor picks an accessible way from 1 to n, and for security reasons it must be the most limited conceivable.
Egor’s chief preferences Dagestan without question and needs to remain here as far as might be feasible, so he requests that you make such timetable that there would be no way from 1 to n or the most limited way’s length would be most prominent conceivable.
A way is one city or a grouping of streets with the end goal that for each street (barring the first) the city this street goes from is equivalent to the city past street goes into. Egor can move just along ways comprising of safe streets as it were.
The way length is equivalent to the quantity of streets in it. The most limited way in a diagram is a way with littlest length.
Information
The primary line contains two numbers n, m (1≤n≤500000, 0≤m≤500000) — the quantity of urban communities and the quantity of streets.
The I-th of next m lines contains three numbers — ui, vi and ti (1≤ui,vi≤n, ti∈{0,1}) — quantities of urban areas associated by street and its sort, individually (0 — night street, 1 — morning street).
Yield
In the primary line yield the length of the ideal way (or −1, if it’s conceivable to pick such timetable that there’s no way from 1 to n).
In the subsequent line yield the ideal timetable — a line of n digits, where I-th digit is 0, if the I-th city is a night one, and 1 if it’s a morning one.
On the off chance that there are numerous answers, print any.
Models
inputCopy
3 4
1 2 0
1 3 1
2 3 0
2 3 1
outputCopy
2
011
inputCopy
4 8
1 0
1 3 0
1 3 1
3 2 0
2 1 0
3 4 1
2 4 0
2 4 1
outputCopy
3
1101
inputCopy
5 10
1 2 0
1 3 1
1 4 0
2 3 0
2 3 1
2 5 0
3 4 0
3 4 1
4 2 1
4 5 0
outputCopy
– 1
11111
Note
For the principal test, in the event that we paint city 1 white, the briefest way is 1→3. Something else, it’s 1→2→3 paying little mind to other urban communities’ hues.
For the subsequent example, we should paint city 3 dark, and there are both high contrast streets going from 2 to 4. Note that there can be a street associating a city with itself.

### 1 thought on “E. Egor in the Republic of Dagestan SOLUTIONS Codeforces Round #669 (Div. 2)”

1. #pragma GCC optimize("Ofast")

#include

#define LLI long long int
#define LD long double
#define PB push_back
#define MP make_pair
#define FORi(i, a, b) for(int i = a; i < b ; ++i)
#define FORd(i, a, b) for(int i = a; i > b ; –i)

using namespace std;

int main(){
int t,n,k,u,v;
vector d1(2000000);
vector d2(2000000);
vector used1_odd,used1_ev,used2_odd,used2_ev;
LLI ans;
cin >> t;
while (t–){
cin >> n >> k;
FORi(i,0,n+n-1)
d1[i]=d2[i]=0;
FORi(i,0,k){
cin >> u >> v;
u–; v–;
d1[u+v]=1;
d2[u-v+n-1]=1;
}
ans=1ll*n*n;
FORi(i,0,n+n-1){
if (d1[i]){
ans-=(i<n?i+1:n+n-1-i);
if (i&1)
used1_odd.PB((i<n?i+1:n+n-1-i));
else
used1_ev.PB((i<n?i+1:n+n-1-i));
}
if (d2[i]){
ans-=(i<n?i+1:n+n-1-i);
if (i&1)
used2_odd.PB((i<n?i+1:n+n-1-i));
else
used2_ev.PB((i<n?i+1:n+n-1-i));
}
}
sort(used2_odd.begin(),used2_odd.end());
sort(used2_ev.begin(),used2_ev.end());
FORi(i,0,used1_odd.size())
if (n&1)
ans+=used2_odd.end()-lower_bound(used2_odd.begin(),used2_odd.end(),n+1-used1_odd[i]);
else
ans+=used2_ev.end()-lower_bound(used2_ev.begin(),used2_ev.end(),n+1-used1_odd[i]);
FORi(i,0,used1_ev.size())
if ((n&1)==0)
ans+=used2_odd.end()-lower_bound(used2_odd.begin(),used2_odd.end(),n+1-used1_ev[i]);
else
ans+=used2_ev.end()-lower_bound(used2_ev.begin(),used2_ev.end(),n+1-used1_ev[i]);
used1_odd.clear();
used1_ev.clear();
used2_odd.clear();
used2_ev.clear();
cout << ans << endl;
}
return 0;
}

Reply