# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
163196 | TadijaSebez | 우호 조약 체결 (JOI14_friends) | C++11 | 148 ms | 12268 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;
#define pb push_back
#define ll long long
const int N=100050;
bool was[N];
vector<pair<int,int>> edges;
vector<int> E[N];
int p[N];
int Find(int x){ return p[x]==x?x:p[x]=Find(p[x]);}
void Union(int x, int y){ p[Find(x)]=Find(y);}
void DFS(int u, int w)
{
was[u]=1;
for(int v:E[u])
{
Union(w,v);
if(!was[v]) DFS(v,w);
}
}
int cnt[N];
int main()
{
int n,m;
scanf("%i %i",&n,&m);
for(int i=1;i<=n;i++) p[i]=i;
for(int i=1,u,v;i<=m;i++) scanf("%i %i",&u,&v),E[u].pb(v),edges.pb({u,v});
for(int i=1;i<=n;i++) if(E[i].size()>1)
{
int w=E[i][0];
for(int v:E[i])
{
Union(v,w);
if(!was[v]) DFS(v,w);
}
}
ll ans=0;
for(auto e:edges)
{
int u,v;tie(u,v)=e;
if(Find(u)!=Find(v)) ans++;
}
for(int i=1;i<=n;i++) cnt[Find(i)]++;
for(int i=1;i<=n;i++) ans+=(ll)cnt[i]*(cnt[i]-1);
printf("%lld\n",ans);
return 0;
}
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... |