# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
199494 | arnold518 | 우호 조약 체결 (JOI14_friends) | C++14 | 162 ms | 10048 KiB |
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>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
int N, M;
ll ans;
vector<int> adj[MAXN+10], adj2[MAXN+10];
int par[MAXN+10], sz[MAXN+10];
int Find(int x) { return x==par[x] ? x : par[x]=Find(par[x]); }
void Union(int x, int y) { x=Find(x); y=Find(y); sz[y]+=sz[x]; par[x]=y; }
bool vis[MAXN+10];
int main()
{
int i, j;
scanf("%d%d", &N, &M);
for(i=1; i<=N; i++) par[i]=i, sz[i]=1;
for(i=1; i<=M; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
}
for(i=1; i<=N; i++)
{
for(j=1; j<adj[i].size(); j++)
{
if(Find(adj[i][0])==Find(adj[i][j])) continue;
Union(adj[i][0], adj[i][j]);
}
}
for(i=1; i<=N; i++)
{
for(auto it : adj[i])
{
int u=i, v=it;
u=Find(u), v=Find(v);
if(u!=v) adj2[u].push_back(v);
}
}
set<pii, greater<pii>> S;
for(i=1; i<=N; i++) if(i==Find(i)) S.insert({sz[i], i});
while(!S.empty())
{
pii t=*S.begin(); S.erase(S.begin());
if(t.first<=1) continue;
for(auto nxt : adj2[t.second])
{
S.erase({sz[Find(nxt)], nxt});
if(Find(t.second)==Find(nxt)) continue;
Union(t.second, nxt);
S.insert({sz[Find(nxt)], nxt});
}
}
for(i=1; i<=N; i++)
{
for(auto it : adj[i])
{
int u=i, v=it;
if(Find(u)!=Find(v)) ans++;
}
}
for(i=1; i<=N; i++) if(i==Find(i)) ans+=(ll)sz[i]*(sz[i]-1);
printf("%lld", ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |