Submission #163196

#TimeUsernameProblemLanguageResultExecution timeMemory
163196TadijaSebez우호 조약 체결 (JOI14_friends)C++11
100 / 100
148 ms12268 KiB
#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)

friends.cpp: In function 'int main()':
friends.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~
friends.cpp:27:59: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1,u,v;i<=m;i++) scanf("%i %i",&u,&v),E[u].pb(v),edges.pb({u,v});
                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...