Submission #71523

# Submission time Handle Problem Language Result Execution time Memory
71523 2018-08-25T02:32:13 Z KLPP Simurgh (IOI17_simurgh) C++14
51 / 100
348 ms 7208 KB
#include "simurgh.h"
#include<vector>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int,int> pi;
vector<pi> nei[100];
int parent[500];
int size[500];
void init(int n){
	for(int i=0;i<n;i++){
		size[i]=1;parent[i]=i;
	}
}
int root(int a){
	if(parent[a]==a)return a;
	return root(parent[a]);
}
void merge(int a, int b){/*cout<<a<<" "<<b<<endl;
	for(int i=0;i<7;i++)cout<<parent[i]<<" ";
	cout<<endl;
	for(int i=0;i<7;i++)cout<<size[i]<<" ";
	cout<<endl<<endl;*/
	a=root(a);
	b=root(b);
	if(a==b)return;
	if(size[a]<size[b]){
		parent[a]=b;
		size[b]+=size[a];
		return;
	}
	parent[b]=a;
	size[a]+=size[b];
}
bool samecomponent(int a, int b){/*cout<<a<<" "<<b<<endl;
	for(int i=0;i<7;i++)cout<<parent[i]<<" ";
	cout<<endl;
	for(int i=0;i<7;i++)cout<<size[i]<<" ";
	cout<<endl<<endl;*/
	a=root(a);
	b=root(b);
	if(a==b)return true;
	return false;
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
	vector<pair<int,int> >nei[n];
	
	for(int i=0;i<u.size();i++){
		nei[u[i]].push_back(pair<int,int>(v[i],i));
		nei[v[i]].push_back(pair<int,int>(u[i],i));
	}
	int ans[u.size()];
	for(int i=0;i<u.size();i++)ans[i]=-1;
	vector<pair<int,int> >tree[n];
	init(n);
	bool istree[u.size()];
	vector<int> maintree;
	for(int i=0;i<u.size();i++){istree[i]=false;
		if(!samecomponent(u[i],v[i])){istree[i]=true;
			merge(u[i],v[i]);
			tree[u[i]].push_back(pair<int,int>(v[i],i));
			tree[v[i]].push_back(pair<int,int>(u[i],i));
			//cout<<i<<endl;
			maintree.push_back(i);
		}
	}
	int s=count_common_roads(maintree);
	for(int i=0;i<u.size();i++){
		if(!istree[i]){
			queue<int> q;
			int dist[n];
			for(int i=0;i<n;i++)dist[i]=-1;
			dist[u[i]]=0;
			q.push(u[i]);
			while(!q.empty()){
				int node=q.front();q.pop();
				for(int j=0;j<tree[node].size();j++){
					int V=tree[node][j].first;
					if(dist[V]==-1){
						dist[V]=dist[node]+1;
						q.push(V);
					}
				}
			}
			vector<int> edges;
			int pnt=v[i];
			while(dist[pnt]!=0){
				for(int j=0;j<tree[pnt].size();j++){
					int node=tree[pnt][j].first;
					if(dist[node]<dist[pnt]){
						edges.push_back(tree[pnt][j].second);
						pnt=node;
						j=10000;
					}
				}
			}//OK
			/*cout<<"Edge no "<<i<<" : ";
			for(int j=0;j<edges.size();j++)cout<<edges[j]<<" ";
			cout<<endl;*/
			vector<int> indeterminado;
			vector<int> determinado;
			for(int j=0;j<edges.size();j++){
				if(ans[edges[j]]==-1)indeterminado.push_back(edges[j]);
				else determinado.push_back(edges[j]);
			}
			if(determinado.size()==0){
				vector<pair<int,int> >respostas;
				for(int j=0;j<edges.size();j++){
					int lo,hi;
					lo=-1;
					hi=n;
					while(hi-lo>1){
						int mid=(hi+lo)/2;
						if(maintree[mid]<edges[j])lo=mid;
						else hi=mid;
					}
					maintree[hi]=i;
					int A=count_common_roads(maintree);
					respostas.push_back(pair<int,int>(A,edges[j]));
					maintree[hi]=edges[j];
				}
				respostas.push_back(pair<int,int>(s,i));
				sort(respostas.begin(),respostas.end());
				
				/*for(int j=0;j<respostas.size();j++){
					cout<<respostas[j].first<<" ";
				}cout<<endl;
				for(int j=0;j<respostas.size();j++){
					cout<<respostas[j].second<<" ";
				}cout<<endl;*/
				if(respostas[0].first==respostas[respostas.size()-1].first){
					for(int j=0;j<respostas.size();j++){
						ans[respostas[j].second]=0;
					}
				}else{
					for(int j=0;j<respostas.size();j++){ans[respostas[j].second]=0;
						if(respostas[j].first==respostas[0].first){
							ans[respostas[j].second]=1;
						}
					}
				}
			}else{	
				
				vector<pair<int,int> >respostas;
				for(int j=0;j<indeterminado.size();j++){//cout<<indeterminado[j]<<" "<<ans[indeterminado[j]]<<endl;
					int lo,hi;
					lo=-1;
					hi=n;
					while(hi-lo>1){
						int mid=(hi+lo)/2;
						if(maintree[mid]<indeterminado[j])lo=mid;
						else hi=mid;
					}//cout<<maintree[hi]<<" "<<indeterminado[j]<<endl;
					maintree[hi]=i;
					/*for(int i=0;i<n-1;i++){
						cout<<maintree[i]<<" ";
					}cout<<endl;*/
					int A=count_common_roads(maintree);
					//cout<<A<<endl;
					respostas.push_back(pair<int,int>(A,indeterminado[j]));
					maintree[hi]=indeterminado[j];
				}//cout<<endl;
				respostas.push_back(pair<int,int>(s,i));
				//cout<<determinado[0]<<" "<<indeterminado.size()<<endl;
				int lo,hi;
				lo=-1;
				hi=n;
				while(hi-lo>1){
					int mid=(hi+lo)/2;
					if(maintree[mid]<determinado[0])lo=mid;
					else hi=mid;
				}
				maintree[hi]=i;
				int reference=count_common_roads(maintree);
				maintree[hi]=determinado[0];
				for(int j=0;j<respostas.size();j++){//cout<<respostas[j].first<<" "<<respostas[j].second<<endl;
					ans[respostas[j].second]=ans[determinado[0]]+reference-respostas[j].first;
				}
			}
		}
		/*cout<<i<<endl;
		for(int i=0;i<u.size();i++){
			cout<<ans[i]<<" ";
		}cout<<endl;
		for(int i=0;i<n-1;i++){
			cout<<maintree[i]<<" ";
		}cout<<endl;*/
	}
	/*for(int i=0;i<u.size();i++){
		cout<<ans[i]<<" ";
	}cout<<endl;*/
	
	vector<int> r;
	for(int i=0;i<u.size();i++){
		if(ans[i]!=0)r.push_back(i);
	}
	return r;
}

Compilation message

simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:49:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<u.size();i++){
              ~^~~~~~~~~
simurgh.cpp:54:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<u.size();i++)ans[i]=-1;
              ~^~~~~~~~~
simurgh.cpp:59:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<u.size();i++){istree[i]=false;
              ~^~~~~~~~~
simurgh.cpp:69:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<u.size();i++){
              ~^~~~~~~~~
simurgh.cpp:78:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<tree[node].size();j++){
                 ~^~~~~~~~~~~~~~~~~~
simurgh.cpp:89:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<tree[pnt].size();j++){
                 ~^~~~~~~~~~~~~~~~~
simurgh.cpp:103:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int j=0;j<edges.size();j++){
                ~^~~~~~~~~~~~~
simurgh.cpp:109:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<edges.size();j++){
                 ~^~~~~~~~~~~~~
simurgh.cpp:133:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int j=0;j<respostas.size();j++){
                  ~^~~~~~~~~~~~~~~~~
simurgh.cpp:137:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int j=0;j<respostas.size();j++){ans[respostas[j].second]=0;
                  ~^~~~~~~~~~~~~~~~~
simurgh.cpp:146:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<indeterminado.size();j++){//cout<<indeterminado[j]<<" "<<ans[indeterminado[j]]<<endl;
                 ~^~~~~~~~~~~~~~~~~~~~~
simurgh.cpp:177:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<respostas.size();j++){//cout<<respostas[j].first<<" "<<respostas[j].second<<endl;
                 ~^~~~~~~~~~~~~~~~~
simurgh.cpp:195:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<u.size();i++){
              ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB correct
2 Correct 3 ms 488 KB correct
3 Correct 2 ms 496 KB correct
4 Correct 2 ms 628 KB correct
5 Correct 4 ms 628 KB correct
6 Correct 3 ms 628 KB correct
7 Correct 2 ms 628 KB correct
8 Correct 3 ms 628 KB correct
9 Correct 3 ms 628 KB correct
10 Correct 3 ms 628 KB correct
11 Correct 3 ms 628 KB correct
12 Correct 3 ms 628 KB correct
13 Correct 4 ms 664 KB correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB correct
2 Correct 3 ms 488 KB correct
3 Correct 2 ms 496 KB correct
4 Correct 2 ms 628 KB correct
5 Correct 4 ms 628 KB correct
6 Correct 3 ms 628 KB correct
7 Correct 2 ms 628 KB correct
8 Correct 3 ms 628 KB correct
9 Correct 3 ms 628 KB correct
10 Correct 3 ms 628 KB correct
11 Correct 3 ms 628 KB correct
12 Correct 3 ms 628 KB correct
13 Correct 4 ms 664 KB correct
14 Correct 7 ms 668 KB correct
15 Correct 6 ms 676 KB correct
16 Correct 6 ms 676 KB correct
17 Correct 7 ms 692 KB correct
18 Correct 4 ms 700 KB correct
19 Correct 7 ms 868 KB correct
20 Correct 6 ms 868 KB correct
21 Correct 6 ms 868 KB correct
22 Correct 4 ms 868 KB correct
23 Correct 4 ms 868 KB correct
24 Correct 5 ms 868 KB correct
25 Correct 2 ms 868 KB correct
26 Correct 6 ms 868 KB correct
27 Correct 5 ms 868 KB correct
28 Correct 5 ms 868 KB correct
29 Correct 4 ms 868 KB correct
30 Correct 4 ms 868 KB correct
31 Correct 6 ms 868 KB correct
32 Correct 4 ms 880 KB correct
33 Correct 5 ms 880 KB correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB correct
2 Correct 3 ms 488 KB correct
3 Correct 2 ms 496 KB correct
4 Correct 2 ms 628 KB correct
5 Correct 4 ms 628 KB correct
6 Correct 3 ms 628 KB correct
7 Correct 2 ms 628 KB correct
8 Correct 3 ms 628 KB correct
9 Correct 3 ms 628 KB correct
10 Correct 3 ms 628 KB correct
11 Correct 3 ms 628 KB correct
12 Correct 3 ms 628 KB correct
13 Correct 4 ms 664 KB correct
14 Correct 7 ms 668 KB correct
15 Correct 6 ms 676 KB correct
16 Correct 6 ms 676 KB correct
17 Correct 7 ms 692 KB correct
18 Correct 4 ms 700 KB correct
19 Correct 7 ms 868 KB correct
20 Correct 6 ms 868 KB correct
21 Correct 6 ms 868 KB correct
22 Correct 4 ms 868 KB correct
23 Correct 4 ms 868 KB correct
24 Correct 5 ms 868 KB correct
25 Correct 2 ms 868 KB correct
26 Correct 6 ms 868 KB correct
27 Correct 5 ms 868 KB correct
28 Correct 5 ms 868 KB correct
29 Correct 4 ms 868 KB correct
30 Correct 4 ms 868 KB correct
31 Correct 6 ms 868 KB correct
32 Correct 4 ms 880 KB correct
33 Correct 5 ms 880 KB correct
34 Correct 324 ms 2072 KB correct
35 Correct 305 ms 2272 KB correct
36 Correct 226 ms 2272 KB correct
37 Correct 15 ms 2272 KB correct
38 Correct 306 ms 2668 KB correct
39 Correct 276 ms 2688 KB correct
40 Correct 234 ms 2732 KB correct
41 Correct 300 ms 3124 KB correct
42 Correct 348 ms 3324 KB correct
43 Correct 169 ms 3324 KB correct
44 Correct 134 ms 3324 KB correct
45 Correct 146 ms 3324 KB correct
46 Correct 157 ms 3324 KB correct
47 Correct 47 ms 3324 KB correct
48 Correct 5 ms 3324 KB correct
49 Correct 17 ms 3324 KB correct
50 Correct 50 ms 3324 KB correct
51 Correct 147 ms 3364 KB correct
52 Correct 114 ms 3364 KB correct
53 Correct 114 ms 3432 KB correct
54 Correct 147 ms 3860 KB correct
55 Correct 160 ms 3860 KB correct
56 Correct 145 ms 3860 KB correct
57 Correct 143 ms 3928 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3928 KB correct
2 Correct 4 ms 3928 KB correct
3 Incorrect 238 ms 7208 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB correct
2 Correct 3 ms 488 KB correct
3 Correct 2 ms 496 KB correct
4 Correct 2 ms 628 KB correct
5 Correct 4 ms 628 KB correct
6 Correct 3 ms 628 KB correct
7 Correct 2 ms 628 KB correct
8 Correct 3 ms 628 KB correct
9 Correct 3 ms 628 KB correct
10 Correct 3 ms 628 KB correct
11 Correct 3 ms 628 KB correct
12 Correct 3 ms 628 KB correct
13 Correct 4 ms 664 KB correct
14 Correct 7 ms 668 KB correct
15 Correct 6 ms 676 KB correct
16 Correct 6 ms 676 KB correct
17 Correct 7 ms 692 KB correct
18 Correct 4 ms 700 KB correct
19 Correct 7 ms 868 KB correct
20 Correct 6 ms 868 KB correct
21 Correct 6 ms 868 KB correct
22 Correct 4 ms 868 KB correct
23 Correct 4 ms 868 KB correct
24 Correct 5 ms 868 KB correct
25 Correct 2 ms 868 KB correct
26 Correct 6 ms 868 KB correct
27 Correct 5 ms 868 KB correct
28 Correct 5 ms 868 KB correct
29 Correct 4 ms 868 KB correct
30 Correct 4 ms 868 KB correct
31 Correct 6 ms 868 KB correct
32 Correct 4 ms 880 KB correct
33 Correct 5 ms 880 KB correct
34 Correct 324 ms 2072 KB correct
35 Correct 305 ms 2272 KB correct
36 Correct 226 ms 2272 KB correct
37 Correct 15 ms 2272 KB correct
38 Correct 306 ms 2668 KB correct
39 Correct 276 ms 2688 KB correct
40 Correct 234 ms 2732 KB correct
41 Correct 300 ms 3124 KB correct
42 Correct 348 ms 3324 KB correct
43 Correct 169 ms 3324 KB correct
44 Correct 134 ms 3324 KB correct
45 Correct 146 ms 3324 KB correct
46 Correct 157 ms 3324 KB correct
47 Correct 47 ms 3324 KB correct
48 Correct 5 ms 3324 KB correct
49 Correct 17 ms 3324 KB correct
50 Correct 50 ms 3324 KB correct
51 Correct 147 ms 3364 KB correct
52 Correct 114 ms 3364 KB correct
53 Correct 114 ms 3432 KB correct
54 Correct 147 ms 3860 KB correct
55 Correct 160 ms 3860 KB correct
56 Correct 145 ms 3860 KB correct
57 Correct 143 ms 3928 KB correct
58 Correct 2 ms 3928 KB correct
59 Correct 4 ms 3928 KB correct
60 Incorrect 238 ms 7208 KB WA in grader: NO
61 Halted 0 ms 0 KB -