# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
43270 | 2018-03-12T12:55:13 Z | ffresh | 우호 조약 체결 (JOI14_friends) | C++14 | 148 ms | 14700 KB |
#include <bits/stdc++.h> using namespace std; const int N = 2e5+15; long long par[N],sz[N],esz[N]; vector<int> adj[N]; int f(int a){ if(a==par[a])return a; return par[a]= f(par[a]); } void merge(int a,int b){ a= f(a),b= f(b); if(a!=b){ par[b]= a; sz[a]+=sz[b]; esz[a]+=esz[b]; } ++esz[a]; } bool vis[N]; void dfs(int root){ vis[root]= 1; for(int i=0;i<adj[root].size();++i){ int u = adj[root][i]; if(!vis[u]){ dfs(u); } merge(u,root); } } int main(){ //freopen("input.txt","r",stdin); int n,m,a,b; scanf("%d%d",&n,&m); for(int i=0;i<m;++i){ scanf("%d%d",&a,&b); adj[a].push_back(b); } for(int i=1;i<=n;++i) par[i] =i,sz[i]= 1; for(int i=1;i<=n;++i){ if((int)adj[i].size()>=2){ int cur = adj[i][0],v; for(int j=0;j<adj[i].size();++j){ int u = adj[i][j]; if(!vis[u]) dfs(u); if(f(u)!=f(cur)){ v = f(cur),u = f(u); par[u] = v; esz[v]+=esz[u]; sz[v] += sz[u]; } } } } long long ret=0; for(int i=1;i<=n;++i){ if(f(i)==i){ ret += (long long)(sz[i]*(sz[i]-1)); ret -= esz[i]; } } ret+=m; cout<<ret<<endl; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 5 ms | 4984 KB | Output is correct |
2 | Correct | 5 ms | 5088 KB | Output is correct |
3 | Correct | 6 ms | 5160 KB | Output is correct |
4 | Correct | 5 ms | 5232 KB | Output is correct |
5 | Correct | 5 ms | 5256 KB | Output is correct |
6 | Correct | 5 ms | 5304 KB | Output is correct |
7 | Correct | 6 ms | 5340 KB | Output is correct |
8 | Correct | 6 ms | 5356 KB | Output is correct |
9 | Correct | 5 ms | 5356 KB | Output is correct |
10 | Correct | 6 ms | 5356 KB | Output is correct |
11 | Correct | 5 ms | 5356 KB | Output is correct |
12 | Correct | 5 ms | 5356 KB | Output is correct |
13 | Correct | 6 ms | 5356 KB | Output is correct |
14 | Correct | 6 ms | 5356 KB | Output is correct |
15 | Correct | 5 ms | 5356 KB | Output is correct |
16 | Correct | 5 ms | 5356 KB | Output is correct |
17 | Correct | 7 ms | 5356 KB | Output is correct |
18 | Correct | 6 ms | 5356 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 7 ms | 5484 KB | Output is correct |
2 | Correct | 6 ms | 5484 KB | Output is correct |
3 | Correct | 13 ms | 5740 KB | Output is correct |
4 | Correct | 52 ms | 6664 KB | Output is correct |
5 | Correct | 46 ms | 6664 KB | Output is correct |
6 | Correct | 68 ms | 7020 KB | Output is correct |
7 | Correct | 7 ms | 7020 KB | Output is correct |
8 | Correct | 9 ms | 7020 KB | Output is correct |
9 | Correct | 13 ms | 7020 KB | Output is correct |
10 | Correct | 24 ms | 7020 KB | Output is correct |
11 | Correct | 67 ms | 7020 KB | Output is correct |
12 | Correct | 11 ms | 7020 KB | Output is correct |
13 | Correct | 7 ms | 7020 KB | Output is correct |
14 | Correct | 7 ms | 7020 KB | Output is correct |
15 | Correct | 9 ms | 7020 KB | Output is correct |
16 | Correct | 17 ms | 7020 KB | Output is correct |
17 | Correct | 69 ms | 7020 KB | Output is correct |
18 | Correct | 7 ms | 7020 KB | Output is correct |
19 | Correct | 7 ms | 7020 KB | Output is correct |
20 | Correct | 67 ms | 7020 KB | Output is correct |
21 | Correct | 8 ms | 7020 KB | Output is correct |
22 | Correct | 7 ms | 7020 KB | Output is correct |
23 | Correct | 10 ms | 7020 KB | Output is correct |
24 | Correct | 8 ms | 7020 KB | Output is correct |
25 | Correct | 8 ms | 7020 KB | Output is correct |
26 | Correct | 8 ms | 7020 KB | Output is correct |
27 | Correct | 7 ms | 7020 KB | Output is correct |
28 | Correct | 7 ms | 7020 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 62 ms | 7020 KB | Output is correct |
2 | Correct | 148 ms | 11756 KB | Output is correct |
3 | Correct | 132 ms | 11756 KB | Output is correct |
4 | Correct | 72 ms | 11756 KB | Output is correct |
5 | Correct | 36 ms | 11756 KB | Output is correct |
6 | Correct | 127 ms | 11756 KB | Output is correct |
7 | Correct | 117 ms | 11756 KB | Output is correct |
8 | Correct | 92 ms | 11756 KB | Output is correct |
9 | Correct | 64 ms | 11756 KB | Output is correct |
10 | Correct | 83 ms | 11756 KB | Output is correct |
11 | Correct | 142 ms | 11884 KB | Output is correct |
12 | Correct | 132 ms | 12700 KB | Output is correct |
13 | Correct | 50 ms | 12700 KB | Output is correct |
14 | Correct | 77 ms | 12700 KB | Output is correct |
15 | Correct | 36 ms | 12700 KB | Output is correct |
16 | Correct | 9 ms | 12700 KB | Output is correct |
17 | Correct | 7 ms | 12700 KB | Output is correct |
18 | Correct | 138 ms | 12700 KB | Output is correct |
19 | Correct | 131 ms | 12700 KB | Output is correct |
20 | Correct | 65 ms | 12700 KB | Output is correct |
21 | Correct | 32 ms | 12700 KB | Output is correct |
22 | Correct | 7 ms | 12700 KB | Output is correct |
23 | Correct | 38 ms | 12700 KB | Output is correct |
24 | Correct | 75 ms | 12700 KB | Output is correct |
25 | Correct | 56 ms | 12700 KB | Output is correct |
26 | Correct | 60 ms | 14700 KB | Output is correct |
27 | Correct | 129 ms | 14700 KB | Output is correct |
28 | Correct | 64 ms | 14700 KB | Output is correct |
29 | Correct | 65 ms | 14700 KB | Output is correct |
30 | Correct | 134 ms | 14700 KB | Output is correct |
31 | Correct | 64 ms | 14700 KB | Output is correct |
32 | Correct | 45 ms | 14700 KB | Output is correct |
33 | Correct | 52 ms | 14700 KB | Output is correct |
34 | Correct | 57 ms | 14700 KB | Output is correct |
35 | Correct | 54 ms | 14700 KB | Output is correct |
36 | Correct | 57 ms | 14700 KB | Output is correct |
37 | Correct | 57 ms | 14700 KB | Output is correct |