제출 #821354

#제출 시각아이디문제언어결과실행 시간메모리
821354danikoynovMarshmallow Molecules (CCO19_day2problem2)C++14
25 / 25
108 ms26700 KiB
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 1e5 + 10;
int n, m;

vector < int > adj[maxn];
set < int > st[maxn];
void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i ++)
    {
        int a, b;
        cin >> a >> b;
        st[a].insert(b);
    }

    ll ans = 0;
    for (int i = 1; i <= n; i ++)
    {
        while(!st[i].empty() && *st[i].begin() <= i)
        {
            st[i].erase(st[i].begin());
        }
        if (st[i].empty())
            continue;

        ///for (int u : st[i])
           /// cout << i << " : " << u << endl;
        ans = ans + (ll)(st[i].size());
        int u = *st[i].begin();

        if (st[u].size() < st[i].size())
            swap(st[u], st[i]);

        for (int v : st[i])
        {
            st[u].insert(v);
        }
    }

    cout << ans << endl;
}

int main()
{
    solve();
    return 0;
}
/**
5 3

1 2
1 3
3 4
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...