-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCF_1363C.cpp
More file actions
105 lines (83 loc) · 1.48 KB
/
CF_1363C.cpp
File metadata and controls
105 lines (83 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
const string res[2] = { "Ayush", "Ashish" };
int graph[1001][1001];
int indegree[1001];
bool deleted[1001];
int n, x;
string solve() {
int turn = 0;
// 시작부터 리프노드이면
if (indegree[x] <= 1)
{
return res[turn];
}
priority_queue<pair<int, int> > pq;
for (int i = 1; i <= n; i++)
{
pq.push(make_pair(-indegree[i], i));
}
while (!pq.empty())
{
int node = pq.top().second;
pq.pop();
if (node == x)
{
turn = 1 - turn;
break;
}
// 제거됨
if (deleted[node])
{
continue;
}
// 제거해서 x 가 리프 노드가 되면 제거하면 안됨
if (indegree[x] <= 2 && graph[node][x] == 1)
{
pq.push(make_pair(-987654321, node));
continue;
}
// 제거
indegree[node]--;
for (int i = 1; i <= n; i++)
{
if (graph[node][i] == 1)
{
indegree[i]--;
graph[node][i] = 0;
graph[i][node] = 0;
// 새로 추가
pq.push(make_pair(-indegree[i], i));
}
}
deleted[node] = true;
turn = 1 - turn;
}
return res[turn];
}
int main() {
int t;
cin >> t;
while (t--)
{
// init
memset(indegree, 0, sizeof(indegree));
memset(deleted, false, sizeof(deleted));
memset(graph, 0, sizeof(graph));
cin >> n >> x;
for (int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
graph[a][b] = 1;
graph[b][a] = 1;
indegree[a]++;
indegree[b]++;
}
cout << solve() << endl;
}
}