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
#include <bits/stdc++.h>
using namespace std;

int zderzenia[500007];
vector <int> G[500007];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int a;
    cin >> a;

    vector < pair <int, int> > tab1;
    vector < pair <int, int> > tab2;

    int b, c, d;

    for(int i = 0; i < a; i++)
    {
        cin >> b >> c >> d;

        if(b == 1)
        {
            tab1.push_back(make_pair(c, d));
        }
        else
        {
            tab2.push_back(make_pair(c, d));
        }
    }

    for(int i = 0; i < tab1.size(); i++)
    {
        for(int x = 0; x < tab2.size(); x++)
        {
            if(tab1[i].first + tab2[x].second == tab2[x].first + tab1[i].second)
            {
                zderzenia[i]++;
                zderzenia[x + tab1.size()]++;
                G[i].push_back(x + tab1.size());
                G[x + tab1.size()].push_back(i);
            }
        }
    }

    priority_queue < pair <int, int> > Q;

    for(int i = 0; i < tab1.size(); i++)
    {
        if(zderzenia[i] != 0)
        {
            Q.push(make_pair(zderzenia[i], i));
        }
    }

    for(int i = 0; i < tab2.size(); i++)
    {
        if(zderzenia[i + tab1.size()] != 0)
        {
            Q.push(make_pair(zderzenia[i + tab1.size()], tab1.size() + i));
        }
    }

    pair <int, int> p;

    int wyn = 0;

    while(Q.size())
    {
        p = Q.top();
        Q.pop();

        if(p.first == zderzenia[p.second])
        {
            zderzenia[p.second] = 0;
            wyn++;

            for(int i = 0; i < G[p.second].size(); i++)
            {
                zderzenia[G[p.second][i]]--;

                if(zderzenia[G[p.second][i]] != 0)
                {
                    Q.push(make_pair(zderzenia[G[p.second][i]], G[p.second][i]));
                }
            }
        }
    }

    cout << wyn << endl;

    return 0;
}