Submission #72213

# Submission time Handle Problem Language Result Execution time Memory
72213 2018-08-26T06:04:01 Z KLPP Duathlon (APIO18_duathlon) C++14
39 / 100
1000 ms 41220 KB
#include<iostream>
#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;
typedef long long int lld;
lld DP[1000000];
bool visited[1000000];
vector<int>nei[1000000];
lld ans;
bool visited2[1000000];
void DFS(int node,lld dist){//cout<<node+1<<" "<<dist<<endl;
	visited[node]=true;
	lld sum=0;
	//lld square=0;
	//lld totaldist=0;
	for(int i=0;i<nei[node].size();i++){
		int v=nei[node][i];
		if(!visited[v]){
			DFS(v,dist+1);
			sum+=DP[v];
			//square+=DP[v]*DP[v];
		}
	}
	DP[node]=sum+1;
	//ans+=sum*sum-square;
	//cout<<sum*sum-square<<" "<<node+1<<endl;
	//ans+=dist*(dist-1);
}
void DFS2(int node,lld componentsize){
	visited2[node]=true;
	lld sq=componentsize-DP[node];
	sq*=sq;
	for(int i=0;i<nei[node].size();i++){
		int v=nei[node][i];
		if(!visited2[v]){
			DFS2(v,componentsize);
			sq+=DP[v]*DP[v];
		}		
	}
	ans+=(componentsize-1)*(componentsize-1)-sq;
	//cout<<(componentsize-1)*(componentsize-1)-sq<<" "<<node+1<<endl;
}
int main(){
	int n,m;
	cin>>n>>m;
	int edges[m][2];
	
	for(int i=0;i<m;i++){
		int x,y;
		cin>>x>>y;
		x--;y--;
		edges[i][0]=x;
		edges[i][1]=y;
		nei[x].push_back(y);
		nei[y].push_back(x);
	}
	//for(int i=0;i<m;i++)cout<<edges[i][0]<<" "<<edges[i][1]<<endl;
	bool enter=true;
	for(int i=0;i<n;i++){
		if(nei[i].size()>2)enter=false;
	}
	if(enter){
	int component[n];
	
	for(int i=0;i<n;i++){
		component[i]=-1;
		
	}
	lld ans=0;
	int color=0;
	for(int i=0;i<n;i++){
		if(component[i]==-1){
			component[i]=color;
			
			queue<int>q;
			q.push(i);
			bool cycle=false;
			lld size=0;
			while(!q.empty()){size++;
				int u=q.front();q.pop();
				//cout<<u<<" ";
				for(int j=0;j<nei[u].size();j++){
					int v=nei[u][j];
					if(component[v]==-1){
						component[v]=color;
						q.push(v);
						
					}
				}
			}
			color++;//cout<<endl;
			lld edges=0;
			for(int j=0;j<n;j++){
				if(component[j]==color-1)edges+=nei[j].size();
			}
			if(edges==2*size)cycle=true;
			//cout<<size<<" "<<cycle<<" "<<edges<<endl;
			
			if(cycle){
				ans+=(size*(size-1)*(size-2));
			}else{
				ans+=(size*(size-1)*(size-2))/3;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
	}
	if(n>50){
	ans=0;
	for(int i=0;i<n;i++){
		DP[i]=-1;
		visited[i]=false;
		visited2[i]=false;
	}
	for(int i=0;i<n;i++){
		if(!visited[i]){
			DFS(i,0);
			DFS2(i,DP[i]);
		}
	}//for(int i=0;i<n;i++)cout<<DP[i]<<" "<<i+1<<endl;
	cout<<ans<<endl;
	return 0;
	}
	if(n<=50){
	int component[n][n];
	vector<int>graph[n];
	for(int node=0;node<n;node++){
		for(int j=0;j<m;j++){
			if(edges[j][0]!=node && edges[j][1]!=node){//cout<<edges[j][0]<<" "<<edges[j][1]<<" "<<j<<endl;
				graph[edges[j][0]].push_back(edges[j][1]);
				graph[edges[j][1]].push_back(edges[j][0]);
				//cout<<edges[j][0]<<" "<<edges[j][1]<<" "<<j<<endl;
			}
		}
		int color=0;
		for(int i=0;i<n;i++)component[node][i]=-1;
		for(int i=0;i<n;i++){
			if(component[node][i]==-1){
				component[node][i]=color;
				queue<int>q;
				q.push(i);
				while(!q.empty()){
					int u=q.front();q.pop();
					//cout<<u<<" ";
					for(int j=0;j<graph[u].size();j++){
						int v=graph[u][j];
						if(component[node][v]==-1){
							component[node][v]=color;
							q.push(v);
						}
					}
				}
				color++;//cout<<endl;
			}
		}
		for(int i=0;i<n;i++)graph[i].clear();
	}
	int cnt=0;
	for(int s=0;s<n;s++){
		for(int f=0;f<n;f++){
			for(int c=0;c<n;c++){
				if(s!=c && s!=f && f!=c){
					bool b=true;
					for(int i=0;i<n;i++){
						if(i!=c){
						if(component[i][c]!=component[i][s] && component[i][c]!=component[i][f]){
							b=false;
						}
						}
					}cnt+=b;
				}
			}
		}
	}
	cout<<cnt<<endl;
	
	return 0;	
	}
}

Compilation message

count_triplets.cpp: In function 'void DFS(int, lld)':
count_triplets.cpp:19:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<nei[node].size();i++){
              ~^~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'void DFS2(int, lld)':
count_triplets.cpp:36:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<nei[node].size();i++){
              ~^~~~~~~~~~~~~~~~~
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:85:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<nei[u].size();j++){
                 ~^~~~~~~~~~~~~~
count_triplets.cpp:149:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int j=0;j<graph[u].size();j++){
                  ~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23800 KB Output is correct
2 Correct 27 ms 23908 KB Output is correct
3 Correct 25 ms 23908 KB Output is correct
4 Correct 26 ms 23908 KB Output is correct
5 Correct 26 ms 24020 KB Output is correct
6 Correct 25 ms 24020 KB Output is correct
7 Correct 26 ms 24020 KB Output is correct
8 Correct 25 ms 24020 KB Output is correct
9 Correct 26 ms 24020 KB Output is correct
10 Correct 25 ms 24020 KB Output is correct
11 Correct 25 ms 24096 KB Output is correct
12 Correct 26 ms 24096 KB Output is correct
13 Correct 26 ms 24096 KB Output is correct
14 Correct 25 ms 24096 KB Output is correct
15 Correct 25 ms 24096 KB Output is correct
16 Correct 26 ms 24172 KB Output is correct
17 Correct 25 ms 24260 KB Output is correct
18 Correct 26 ms 24260 KB Output is correct
19 Correct 26 ms 24260 KB Output is correct
20 Correct 27 ms 24260 KB Output is correct
21 Correct 25 ms 24260 KB Output is correct
22 Correct 24 ms 24260 KB Output is correct
23 Correct 26 ms 24260 KB Output is correct
24 Correct 25 ms 24260 KB Output is correct
25 Correct 26 ms 24260 KB Output is correct
26 Correct 25 ms 24260 KB Output is correct
27 Correct 25 ms 24260 KB Output is correct
28 Correct 29 ms 24260 KB Output is correct
29 Correct 25 ms 24260 KB Output is correct
30 Correct 24 ms 24260 KB Output is correct
31 Correct 23 ms 24260 KB Output is correct
32 Correct 25 ms 24260 KB Output is correct
33 Correct 24 ms 24260 KB Output is correct
34 Correct 27 ms 24260 KB Output is correct
35 Correct 27 ms 24260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23800 KB Output is correct
2 Correct 27 ms 23908 KB Output is correct
3 Correct 25 ms 23908 KB Output is correct
4 Correct 26 ms 23908 KB Output is correct
5 Correct 26 ms 24020 KB Output is correct
6 Correct 25 ms 24020 KB Output is correct
7 Correct 26 ms 24020 KB Output is correct
8 Correct 25 ms 24020 KB Output is correct
9 Correct 26 ms 24020 KB Output is correct
10 Correct 25 ms 24020 KB Output is correct
11 Correct 25 ms 24096 KB Output is correct
12 Correct 26 ms 24096 KB Output is correct
13 Correct 26 ms 24096 KB Output is correct
14 Correct 25 ms 24096 KB Output is correct
15 Correct 25 ms 24096 KB Output is correct
16 Correct 26 ms 24172 KB Output is correct
17 Correct 25 ms 24260 KB Output is correct
18 Correct 26 ms 24260 KB Output is correct
19 Correct 26 ms 24260 KB Output is correct
20 Correct 27 ms 24260 KB Output is correct
21 Correct 25 ms 24260 KB Output is correct
22 Correct 24 ms 24260 KB Output is correct
23 Correct 26 ms 24260 KB Output is correct
24 Correct 25 ms 24260 KB Output is correct
25 Correct 26 ms 24260 KB Output is correct
26 Correct 25 ms 24260 KB Output is correct
27 Correct 25 ms 24260 KB Output is correct
28 Correct 29 ms 24260 KB Output is correct
29 Correct 25 ms 24260 KB Output is correct
30 Correct 24 ms 24260 KB Output is correct
31 Correct 23 ms 24260 KB Output is correct
32 Correct 25 ms 24260 KB Output is correct
33 Correct 24 ms 24260 KB Output is correct
34 Correct 27 ms 24260 KB Output is correct
35 Correct 27 ms 24260 KB Output is correct
36 Correct 29 ms 24260 KB Output is correct
37 Correct 49 ms 24260 KB Output is correct
38 Correct 40 ms 24260 KB Output is correct
39 Correct 38 ms 24260 KB Output is correct
40 Correct 40 ms 24260 KB Output is correct
41 Correct 41 ms 24260 KB Output is correct
42 Correct 39 ms 24260 KB Output is correct
43 Correct 39 ms 24260 KB Output is correct
44 Correct 41 ms 24260 KB Output is correct
45 Correct 38 ms 24260 KB Output is correct
46 Correct 38 ms 24260 KB Output is correct
47 Correct 26 ms 24260 KB Output is correct
48 Correct 25 ms 24260 KB Output is correct
49 Correct 27 ms 24260 KB Output is correct
50 Correct 27 ms 24260 KB Output is correct
51 Correct 39 ms 24260 KB Output is correct
52 Correct 40 ms 24260 KB Output is correct
53 Correct 39 ms 24260 KB Output is correct
54 Correct 40 ms 24260 KB Output is correct
55 Correct 39 ms 24260 KB Output is correct
56 Correct 37 ms 24260 KB Output is correct
57 Correct 41 ms 24260 KB Output is correct
58 Correct 38 ms 24260 KB Output is correct
59 Correct 44 ms 24260 KB Output is correct
60 Correct 40 ms 24260 KB Output is correct
61 Correct 41 ms 24260 KB Output is correct
62 Correct 40 ms 24260 KB Output is correct
63 Correct 36 ms 24260 KB Output is correct
64 Correct 38 ms 24260 KB Output is correct
65 Correct 37 ms 24260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 194 ms 28376 KB Output is correct
2 Correct 195 ms 28376 KB Output is correct
3 Correct 208 ms 28376 KB Output is correct
4 Correct 195 ms 29816 KB Output is correct
5 Correct 205 ms 30984 KB Output is correct
6 Correct 188 ms 32112 KB Output is correct
7 Correct 188 ms 33544 KB Output is correct
8 Correct 196 ms 34724 KB Output is correct
9 Correct 220 ms 36044 KB Output is correct
10 Correct 200 ms 37284 KB Output is correct
11 Execution timed out 1037 ms 38228 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 38228 KB Output is correct
2 Correct 27 ms 38228 KB Output is correct
3 Correct 26 ms 38228 KB Output is correct
4 Correct 28 ms 38228 KB Output is correct
5 Correct 26 ms 38228 KB Output is correct
6 Correct 30 ms 38228 KB Output is correct
7 Correct 26 ms 38228 KB Output is correct
8 Correct 29 ms 38228 KB Output is correct
9 Correct 29 ms 38228 KB Output is correct
10 Correct 31 ms 38228 KB Output is correct
11 Correct 30 ms 38228 KB Output is correct
12 Correct 25 ms 38228 KB Output is correct
13 Correct 27 ms 38228 KB Output is correct
14 Correct 28 ms 38228 KB Output is correct
15 Correct 28 ms 38228 KB Output is correct
16 Correct 29 ms 38228 KB Output is correct
17 Correct 29 ms 38228 KB Output is correct
18 Correct 27 ms 38228 KB Output is correct
19 Correct 26 ms 38228 KB Output is correct
20 Correct 26 ms 38228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 235 ms 39060 KB Output is correct
2 Correct 237 ms 39072 KB Output is correct
3 Correct 221 ms 39072 KB Output is correct
4 Correct 225 ms 39072 KB Output is correct
5 Correct 212 ms 39072 KB Output is correct
6 Correct 217 ms 39072 KB Output is correct
7 Correct 278 ms 41220 KB Output is correct
8 Correct 307 ms 41220 KB Output is correct
9 Correct 222 ms 41220 KB Output is correct
10 Correct 214 ms 41220 KB Output is correct
11 Correct 205 ms 41220 KB Output is correct
12 Correct 213 ms 41220 KB Output is correct
13 Correct 230 ms 41220 KB Output is correct
14 Correct 184 ms 41220 KB Output is correct
15 Correct 173 ms 41220 KB Output is correct
16 Correct 117 ms 41220 KB Output is correct
17 Correct 159 ms 41220 KB Output is correct
18 Correct 172 ms 41220 KB Output is correct
19 Correct 203 ms 41220 KB Output is correct
20 Correct 178 ms 41220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 41220 KB Output is correct
2 Correct 26 ms 41220 KB Output is correct
3 Incorrect 26 ms 41220 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 210 ms 41220 KB Output is correct
2 Correct 273 ms 41220 KB Output is correct
3 Incorrect 245 ms 41220 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23800 KB Output is correct
2 Correct 27 ms 23908 KB Output is correct
3 Correct 25 ms 23908 KB Output is correct
4 Correct 26 ms 23908 KB Output is correct
5 Correct 26 ms 24020 KB Output is correct
6 Correct 25 ms 24020 KB Output is correct
7 Correct 26 ms 24020 KB Output is correct
8 Correct 25 ms 24020 KB Output is correct
9 Correct 26 ms 24020 KB Output is correct
10 Correct 25 ms 24020 KB Output is correct
11 Correct 25 ms 24096 KB Output is correct
12 Correct 26 ms 24096 KB Output is correct
13 Correct 26 ms 24096 KB Output is correct
14 Correct 25 ms 24096 KB Output is correct
15 Correct 25 ms 24096 KB Output is correct
16 Correct 26 ms 24172 KB Output is correct
17 Correct 25 ms 24260 KB Output is correct
18 Correct 26 ms 24260 KB Output is correct
19 Correct 26 ms 24260 KB Output is correct
20 Correct 27 ms 24260 KB Output is correct
21 Correct 25 ms 24260 KB Output is correct
22 Correct 24 ms 24260 KB Output is correct
23 Correct 26 ms 24260 KB Output is correct
24 Correct 25 ms 24260 KB Output is correct
25 Correct 26 ms 24260 KB Output is correct
26 Correct 25 ms 24260 KB Output is correct
27 Correct 25 ms 24260 KB Output is correct
28 Correct 29 ms 24260 KB Output is correct
29 Correct 25 ms 24260 KB Output is correct
30 Correct 24 ms 24260 KB Output is correct
31 Correct 23 ms 24260 KB Output is correct
32 Correct 25 ms 24260 KB Output is correct
33 Correct 24 ms 24260 KB Output is correct
34 Correct 27 ms 24260 KB Output is correct
35 Correct 27 ms 24260 KB Output is correct
36 Correct 29 ms 24260 KB Output is correct
37 Correct 49 ms 24260 KB Output is correct
38 Correct 40 ms 24260 KB Output is correct
39 Correct 38 ms 24260 KB Output is correct
40 Correct 40 ms 24260 KB Output is correct
41 Correct 41 ms 24260 KB Output is correct
42 Correct 39 ms 24260 KB Output is correct
43 Correct 39 ms 24260 KB Output is correct
44 Correct 41 ms 24260 KB Output is correct
45 Correct 38 ms 24260 KB Output is correct
46 Correct 38 ms 24260 KB Output is correct
47 Correct 26 ms 24260 KB Output is correct
48 Correct 25 ms 24260 KB Output is correct
49 Correct 27 ms 24260 KB Output is correct
50 Correct 27 ms 24260 KB Output is correct
51 Correct 39 ms 24260 KB Output is correct
52 Correct 40 ms 24260 KB Output is correct
53 Correct 39 ms 24260 KB Output is correct
54 Correct 40 ms 24260 KB Output is correct
55 Correct 39 ms 24260 KB Output is correct
56 Correct 37 ms 24260 KB Output is correct
57 Correct 41 ms 24260 KB Output is correct
58 Correct 38 ms 24260 KB Output is correct
59 Correct 44 ms 24260 KB Output is correct
60 Correct 40 ms 24260 KB Output is correct
61 Correct 41 ms 24260 KB Output is correct
62 Correct 40 ms 24260 KB Output is correct
63 Correct 36 ms 24260 KB Output is correct
64 Correct 38 ms 24260 KB Output is correct
65 Correct 37 ms 24260 KB Output is correct
66 Correct 26 ms 41220 KB Output is correct
67 Correct 26 ms 41220 KB Output is correct
68 Incorrect 26 ms 41220 KB Output isn't correct
69 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23800 KB Output is correct
2 Correct 27 ms 23908 KB Output is correct
3 Correct 25 ms 23908 KB Output is correct
4 Correct 26 ms 23908 KB Output is correct
5 Correct 26 ms 24020 KB Output is correct
6 Correct 25 ms 24020 KB Output is correct
7 Correct 26 ms 24020 KB Output is correct
8 Correct 25 ms 24020 KB Output is correct
9 Correct 26 ms 24020 KB Output is correct
10 Correct 25 ms 24020 KB Output is correct
11 Correct 25 ms 24096 KB Output is correct
12 Correct 26 ms 24096 KB Output is correct
13 Correct 26 ms 24096 KB Output is correct
14 Correct 25 ms 24096 KB Output is correct
15 Correct 25 ms 24096 KB Output is correct
16 Correct 26 ms 24172 KB Output is correct
17 Correct 25 ms 24260 KB Output is correct
18 Correct 26 ms 24260 KB Output is correct
19 Correct 26 ms 24260 KB Output is correct
20 Correct 27 ms 24260 KB Output is correct
21 Correct 25 ms 24260 KB Output is correct
22 Correct 24 ms 24260 KB Output is correct
23 Correct 26 ms 24260 KB Output is correct
24 Correct 25 ms 24260 KB Output is correct
25 Correct 26 ms 24260 KB Output is correct
26 Correct 25 ms 24260 KB Output is correct
27 Correct 25 ms 24260 KB Output is correct
28 Correct 29 ms 24260 KB Output is correct
29 Correct 25 ms 24260 KB Output is correct
30 Correct 24 ms 24260 KB Output is correct
31 Correct 23 ms 24260 KB Output is correct
32 Correct 25 ms 24260 KB Output is correct
33 Correct 24 ms 24260 KB Output is correct
34 Correct 27 ms 24260 KB Output is correct
35 Correct 27 ms 24260 KB Output is correct
36 Correct 29 ms 24260 KB Output is correct
37 Correct 49 ms 24260 KB Output is correct
38 Correct 40 ms 24260 KB Output is correct
39 Correct 38 ms 24260 KB Output is correct
40 Correct 40 ms 24260 KB Output is correct
41 Correct 41 ms 24260 KB Output is correct
42 Correct 39 ms 24260 KB Output is correct
43 Correct 39 ms 24260 KB Output is correct
44 Correct 41 ms 24260 KB Output is correct
45 Correct 38 ms 24260 KB Output is correct
46 Correct 38 ms 24260 KB Output is correct
47 Correct 26 ms 24260 KB Output is correct
48 Correct 25 ms 24260 KB Output is correct
49 Correct 27 ms 24260 KB Output is correct
50 Correct 27 ms 24260 KB Output is correct
51 Correct 39 ms 24260 KB Output is correct
52 Correct 40 ms 24260 KB Output is correct
53 Correct 39 ms 24260 KB Output is correct
54 Correct 40 ms 24260 KB Output is correct
55 Correct 39 ms 24260 KB Output is correct
56 Correct 37 ms 24260 KB Output is correct
57 Correct 41 ms 24260 KB Output is correct
58 Correct 38 ms 24260 KB Output is correct
59 Correct 44 ms 24260 KB Output is correct
60 Correct 40 ms 24260 KB Output is correct
61 Correct 41 ms 24260 KB Output is correct
62 Correct 40 ms 24260 KB Output is correct
63 Correct 36 ms 24260 KB Output is correct
64 Correct 38 ms 24260 KB Output is correct
65 Correct 37 ms 24260 KB Output is correct
66 Correct 194 ms 28376 KB Output is correct
67 Correct 195 ms 28376 KB Output is correct
68 Correct 208 ms 28376 KB Output is correct
69 Correct 195 ms 29816 KB Output is correct
70 Correct 205 ms 30984 KB Output is correct
71 Correct 188 ms 32112 KB Output is correct
72 Correct 188 ms 33544 KB Output is correct
73 Correct 196 ms 34724 KB Output is correct
74 Correct 220 ms 36044 KB Output is correct
75 Correct 200 ms 37284 KB Output is correct
76 Execution timed out 1037 ms 38228 KB Time limit exceeded
77 Halted 0 ms 0 KB -