답안 #428204

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
428204 2021-06-15T08:49:51 Z faresbasbs Simurgh (IOI17_simurgh) C++14
0 / 100
4 ms 6220 KB
#include <bits/stdc++.h>
#include "simurgh.h"
using namespace std;
vector<pair<int,int>> edges,graph[501],graph2[501*501];
int n,q,pre,h[501],par[501],par2[501];
bool seen[501],sg[501],seen2[501*501];
vector<int> gp[501];
set<int> used,asked;
vector<int> tp[2];

void dfs3(int curr){
	sg[curr] = 1;
	for(auto i : gp[curr]){
		if(sg[i]){
			continue;
		}
		dfs3(i);
	}
}

int c(){
	if(used.size() != n-1){
		return -1;
	}
	for(int i = 0 ; i < n ; i += 1){
		gp[i].clear();
	}
	for(auto i : used){
		gp[edges[i].first].push_back(edges[i].second);
		gp[edges[i].second].push_back(edges[i].first);
	}
	memset(sg,0,sizeof sg);
	dfs3(0);
	for(int i = 0 ; i < n ; i += 1){
		if(!sg[i]){
			return -1;
		}
	}
	q++;
	assert(q <= 29000);
	vector<int> v;
	for(auto i : used){
		v.push_back(i);
	}
	return count_common_roads(v);
}

void dfs(int curr){
	seen[curr] = 1;
	for(auto i : graph[curr]){
		if(seen[i.first]){
			continue;
		}
		h[i.first] = h[curr]+1;
		used.insert(i.second);
		par[i.first] = curr , par2[i.first] = i.second;
		dfs(i.first);
	}
}

void ask(int a , int b){
	if(asked.count(b) && asked.count(a)){
		return ;
	}
	used.erase(a),used.insert(b);
	int val = c();
	// for(auto i : used){
	// 	cout << i << " ";
	// }cout << endl;
	// cout << val << endl;
	if(pre == val){
		// cout << a << " " << b << " " << 0 << endl;
		graph2[a].push_back({b,0});
		graph2[b].push_back({a,0});
	}else{
		// cout << a << " " << b << " " << 1 << endl;
		graph2[a].push_back({b,1});
		graph2[b].push_back({a,1});
	}
	used.erase(b),used.insert(a);
	asked.insert(a),asked.insert(b);
}

void dfs2(int curr , int val){
	if(used.count(curr)){
		used.erase(curr);
	}
	seen2[curr] = 1;
	tp[val].push_back(curr);
	for(auto i : graph2[curr]){
		if(seen2[i.first]){
			continue;
		}
		dfs2(i.first,val^i.second);
	}
}

vector<int> find_roads(int N , vector<int> from , vector<int> to){
	n = N;
	for(int i = 0 ; i < from.size() ; i += 1){
		edges.push_back({from[i],to[i]});
		int a = from[i] , b = to[i];
		graph[a].push_back({b,i}),graph[b].push_back({a,i});
	}
	dfs(0);
	/*for(auto i : used){
		cout << i << " ";
	}cout << endl;*/
	pre = c();
	for(int i = 0 ; i < from.size() ; i += 1){
		if(used.count(i)){
			continue;
		}
		asked.insert(i);
		int a = from[i] , b = to[i];
		if(h[a] < h[b]){
			swap(a,b);
		}
		ask(par2[a],i);
		a = par[a];
		if(h[a] < h[b]){
			swap(a,b);
		}
		while(h[a] > h[b]){
			ask(par2[a],i);
			a = par[a];
		}
		while(a != b){
			ask(par2[a],i),ask(par2[b],i);
			a = par[a] , b = par[b];
		}
	}
	vector<int> v;
	for(auto i : used){
		v.push_back(i);
	}
	for(auto i : v){
		if(seen2[i] || !asked.count(i)){
			continue;
		}
		tp[0].clear(),tp[1].clear();
		dfs2(i,0);
		for(auto j : tp[0]){
			used.insert(j);
		}
		int val1 = c();
		for(auto j : tp[0]){
			used.erase(j);
		}
		for(auto j : tp[1]){
			used.insert(j);
		}
		int val2 = c();
		if(val2 > val1){
			continue;
		}
		for(auto j : tp[1]){
			used.erase(j);
		}
		for(auto j : tp[0]){
			used.insert(j);
		}
	}
	v.clear();
	for(auto i : used){
		v.push_back(i);
	}
	return v;
}

Compilation message

simurgh.cpp: In function 'int c()':
simurgh.cpp:22:17: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   22 |  if(used.size() != n-1){
      |     ~~~~~~~~~~~~^~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:100:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |  for(int i = 0 ; i < from.size() ; i += 1){
      |                  ~~^~~~~~~~~~~~~
simurgh.cpp:110:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |  for(int i = 0 ; i < from.size() ; i += 1){
      |                  ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 6220 KB WA in grader: NO
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 6220 KB WA in grader: NO
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 6220 KB WA in grader: NO
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6220 KB correct
2 Incorrect 4 ms 6220 KB WA in grader: NO
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 6220 KB WA in grader: NO
2 Halted 0 ms 0 KB -