답안 #581919

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
581919 2022-06-23T08:05:01 Z alireza_kaviani Simurgh (IOI17_simurgh) C++17
0 / 100
1 ms 468 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;

#define SZ(x)	int((x).size())
#define X 		first
#define Y 		second
#define sep 	' '

const ll MAXN = 510; //TODO: fix MAXN

int n , m , common , mark[MAXN] , H[MAXN] , par[MAXN] , ind[MAXN] , tree[MAXN * MAXN] , ans[MAXN * MAXN];
vector<pii> E , adj[MAXN];

int query(int A[]){
	vector<int> vec;
	for(int i = 0 ; i < m ; i++){
		if(A[i]){
			vec.push_back(i);
		}
	}
	assert(SZ(vec) == n - 1);
	return count_common_roads(vec);
}

void DFS(int v){
	mark[v] = 1;
	for(pii i : adj[v]){
		int u = i.X , w = i.Y;
		if(mark[u]){
			continue;
		}
//		cout << v << sep << u << sep << w << endl;
		par[u] = v;
		ind[u] = w;
		tree[w] = 1;
		H[u] = H[v] + 1;
		DFS(u);
	}
}

void Calc(int v , int u , int index){
	int mn = common , W = -1 , x = v;
	vector<pii> res;
	tree[index] = 1;
	while(x != u){
		if(ans[ind[x]] != -1){
			if(W != -1){
				res.push_back({W - ans[ind[x]] , x});
				x = par[x];
				continue;
			}
			tree[ind[x]] = 0;
			int cur = query(tree);
			tree[ind[x]] = 1;
			res.push_back({cur , x});
			W = cur + ans[ind[x]];
			x = par[x];
			continue;
		}
		tree[ind[x]] = 0;
		int cur = query(tree);
		tree[ind[x]] = 1;
		res.push_back({cur , x});
		x = par[x];
	}
	tree[index] = 0;
//	cout << "==========" << endl;
//	cout << v << sep << u << sep << index << endl;
	for(pii i : res){
//		cout << i.X << sep << i.Y << endl;
		mn = min(mn , i.X);
	}
//	cout << mn << endl;
	for(pii i : res){
		if(i.X == mn){
			ans[ind[i.Y]] = 1;
		}
		else{
			ans[ind[i.Y]] = 0;
		}
	}
	if(common == mn){
		ans[index] = 1;
	}
	else{
		ans[index] = 0;
	}
}

vector<int> find_roads(int N, vector<int> U, vector<int> V) {
	fill(ans , ans + MAXN , -1);
	n = N; m = SZ(U);
	for(int i = 0 ; i < m ; i++){
		adj[V[i]].push_back({U[i] , i});
		adj[U[i]].push_back({V[i] , i});
		E.push_back({V[i] , U[i]});
	}
	DFS(0);
	common = query(tree);
	for(int i = 0 ; i < m ; i++){
		if(H[E[i].X] < H[E[i].Y]){
			swap(E[i].X , E[i].Y);
		}
	}
	for(int i = 0 ; i < m ; i++){
		if(tree[i] == 0){
			Calc(E[i].X , E[i].Y , i);
		}
	}
	vector<int> res;
	for(int i = 0 ; i < m ; i++){
		assert(ans[i] != -1);
		if(ans[i]){
			res.push_back(i);
		}
	}
	return res;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB WA in grader: NO
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB WA in grader: NO
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB WA in grader: NO
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB WA in grader: NO
2 Halted 0 ms 0 KB -