This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |