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
#include <iostream>
using namespace std;

struct color
{
    int Dr, Db, Dy, Or, Ob, Oy;
};

color tab[1000009];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++)
    {
        int a, b, k;
        cin >> a >> b >> k;
        if(k == 1)
        {
            tab[a].Dy++;
            tab[b].Oy++;
        }
        else if(k == 2)
        {
            tab[a].Db++;
            tab[b].Ob++;
        }
        else
        {
            tab[a].Dr++;
            tab[b].Or++;
        }
    }
    int res = 0;
    int red, blue, yellow;
    red = blue = yellow = 0;
    for(int i = 1; i <= n; i++)
    {
        red += tab[i].Dr;
        yellow += tab[i].Dy;
        blue += tab[i].Db;

        if(red == 0 && blue > 0 && yellow > 0) res++;
        
        red -= tab[i].Or;
        yellow -= tab[i].Oy;
        blue -= tab[i].Ob;
    }
    cout << res << endl;
}