Submission #2167589


Source Code Expand

#include <iostream>
#include <vector>
#include <utility>
#define inf 1000000000

using namespace std;

struct edge{
	int to, cap, rev;
	edge(int a, int b, int c){
		to = a, cap = b, rev = c;
	}
};

int N;
vector<int> tree[100005];
int depth[100005];
vector<edge> G[100005];
int S, T;
bool used[100005];

void dfs2(int v, int prev, int dep)
{
	depth[v] = dep;
	for(int i = 0; i < tree[v].size(); i++){
		if(tree[v][i] != prev) dfs2(tree[v][i], v, dep+1);
	}
}

int dfs(int v, int f)
{
	used[v] = true;
	if(v == T) return f;
	
	int ret;
	for(int i = 0; i < G[v].size(); i++){
		if(used[G[v][i].to] || G[v][i].cap <= 0) continue;
		ret = dfs(G[v][i].to, min(f, G[v][i].cap));
		if(ret > 0){
			G[v][i].cap -= ret;
			G[G[v][i].to][G[v][i].rev].cap += ret;
			return ret;
		}
	}
	return 0;
}

void add_edge(int s, int t, int cap)
{
	G[s].push_back(edge(t, cap, G[t].size()));
	G[t].push_back(edge(s, 0, G[s].size()-1));
}

int main(void)
{
	cin >> N;
	int a, b;
	for(int i = 0; i < N-1; i++){
		cin >> a >> b;
		tree[a].push_back(b);
		tree[b].push_back(a);
	}
	S = N+1, T = N+2;
	
	dfs2(1, -1, 0);
	for(int i = 1; i <= N; i++){
		if(depth[i] % 2){
			add_edge(S, i, 1);
			for(int j = 0; j < tree[i].size(); j++) add_edge(i, tree[i][j], 1);
		}
		else add_edge(i, T, 1);
	}
	
	int ans = 0, flow;
	while(1){
		for(int i = 1; i <= T; i++) used[i] = false;
		flow = dfs(S, inf);
		if(flow <= 0) break;
		ans += flow;
	}
	
	if(N % 2 == 0 && ans == N/2) cout << "Second" << endl;
	else cout << "First" << endl;
	return 0;
}

Submission Info

Submission Time
Task D - Black and White Tree
User leaf1415
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1599 Byte
Status TLE
Exec Time 2104 ms
Memory 17676 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 900
Status
AC × 3
AC × 8
TLE × 31
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
in1.txt TLE 2104 ms 15216 KB
in10.txt TLE 2104 ms 15216 KB
in11.txt TLE 2104 ms 17648 KB
in12.txt AC 53 ms 6400 KB
in13.txt TLE 2104 ms 17648 KB
in14.txt TLE 2104 ms 15600 KB
in15.txt TLE 2104 ms 15600 KB
in16.txt TLE 2104 ms 15600 KB
in17.txt TLE 2104 ms 17648 KB
in18.txt TLE 2104 ms 15600 KB
in19.txt TLE 2104 ms 15600 KB
in2.txt TLE 2104 ms 15216 KB
in20.txt TLE 2104 ms 17648 KB
in21.txt TLE 2104 ms 15216 KB
in22.txt TLE 2104 ms 15216 KB
in23.txt TLE 2104 ms 15600 KB
in24.txt TLE 2104 ms 15600 KB
in25.txt TLE 2104 ms 17676 KB
in26.txt TLE 2104 ms 15600 KB
in27.txt TLE 2104 ms 15600 KB
in28.txt AC 3 ms 5376 KB
in29.txt TLE 2104 ms 15600 KB
in3.txt TLE 2104 ms 17264 KB
in30.txt TLE 2104 ms 15600 KB
in31.txt TLE 2104 ms 15600 KB
in32.txt TLE 2104 ms 15600 KB
in33.txt TLE 2104 ms 15600 KB
in4.txt TLE 2104 ms 15216 KB
in5.txt TLE 2104 ms 15216 KB
in6.txt TLE 2104 ms 15216 KB
in7.txt TLE 2104 ms 15216 KB
in8.txt TLE 2104 ms 17264 KB
in9.txt TLE 2104 ms 15216 KB
sample1.txt AC 3 ms 5376 KB
sample2.txt AC 3 ms 5376 KB
sample3.txt AC 3 ms 5376 KB