# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
821195 | DobromirAngelov | Marshmallow Molecules (CCO19_day2problem2) | C++17 | 35 ms | 5964 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>
#define endl '\n'
using namespace std;
const int MAXN=1e5+5;
int n,m;
vector<int> adj[MAXN];
bool used[MAXN];
long long ans=0;
void solve(int v)
{
used[v]=1;
priority_queue<int> pq;
pq.push(-v);
while(!pq.empty())
{
v=-pq.top();
pq.pop();
int bef=pq.size();
int br=0;
for(int i=0;i<adj[v].size();i++)
{
if(!used[adj[v][i]])
{
used[adj[v][i]]=1;
pq.push(-adj[v][i]);
br++;
}
}
ans+=br;
ans+=(long long) br*bef;
ans+=(long long) br*(br-1)/2;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
}
for(int i=1;i<=n;i++)
{
if(!used[i])
{
if(adj[i].size()==0) continue;
solve(i);
}
}
cout<<ans<<endl;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |