답안 #581938

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
581938 2022-06-23T08:25:47 Z alireza_kaviani Simurgh (IOI17_simurgh) C++17
13 / 100
2973 ms 4464 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 mx = 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;
		mx = max(mx , i.X);
	}
	for(pii i : res){
		if(i.X == mx){
			ans[ind[i.Y]] = 0;
		}
		else{
			ans[ind[i.Y]] = 1;
		}
	}
	if(common == mx){
		ans[index] = 0;
	}
	else{
		ans[index] = 1;
	}
}

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);
			/*for(int j = 0 ; j < m ; j++){
				cout << ans[j] << sep;
			}
			cout << endl;*/
		}
	}
	for(int i = 0 ; i < m ; i++){
		if(ans[i] == -1){
			ans[i] = 1;
		}
	}
	vector<int> res;
	for(int i = 0 ; i < m ; i++){
		if(ans[i]){
			res.push_back(i);
		}
	}
	assert(SZ(res) <= n - 1);
	return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 1 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 1 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 7 ms 340 KB correct
15 Correct 7 ms 340 KB correct
16 Correct 7 ms 340 KB correct
17 Runtime error 6 ms 596 KB Execution killed with signal 6
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 1 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 7 ms 340 KB correct
15 Correct 7 ms 340 KB correct
16 Correct 7 ms 340 KB correct
17 Runtime error 6 ms 596 KB Execution killed with signal 6
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Incorrect 2973 ms 4464 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 1 ms 212 KB correct
10 Correct 0 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 7 ms 340 KB correct
15 Correct 7 ms 340 KB correct
16 Correct 7 ms 340 KB correct
17 Runtime error 6 ms 596 KB Execution killed with signal 6
18 Halted 0 ms 0 KB -