Codeforces Round #646 (Div. 2) C. Game On Leaves

Codeforces Round #646 (Div. 2) C. Game On Leaves

題目鏈接
Ayush and Ashish play a game on an unrooted tree consisting of n nodes numbered 1 to n. Players make the following move in turns:

Select any leaf node in the tree and remove it together with any edge which has this node as one of its endpoints. A leaf node is a node with degree less than or equal to 1.
A tree is a connected undirected graph without cycles.

There is a special node numbered x. The player who removes this node wins the game.

Ayush moves first. Determine the winner of the game if each player plays optimally.

Input

The first line of the input contains a single integer t (1≤t≤10) — the number of testcases. The description of the test cases follows.

The first line of each testcase contains two integers n and x (1≤n≤1000,1≤x≤n) — the number of nodes in the tree and the special node respectively.

Each of the next n−1 lines contain two integers u, v (1≤u,v≤n, u≠v), meaning that there is an edge between nodes u and v in the tree.

Output

For every test case, if Ayush wins the game, print “Ayush”, otherwise print “Ashish” (without quotes).

Examples

input

1
3 1
2 1
3 1

output

Ashish

input

1
3 2
1 2
1 3

output

Ayush

樹上 DFS +博弈 ~
題目實際上不難,要敢於亂猜😀,我們就以 xx 爲根節點,DFS求出其所有孩子的個數,我們不難發現,當 xx 上只有小於等於 1 條鏈連接時,就能分出勝負。我構造了幾個樣例發現鏈長爲偶數時對結果沒有影響,關鍵就在於奇數鏈的個數,如果爲奇數則 AyushAyush 贏,否則 AshishAshish 贏,AC代碼如下:

#include<bits/stdc++.h>
using namespace std;
vector<int>g[1005];
int s[1005],t,n,x,u,v;;
void dfs(int u,int fa){
    for(int v:g[u]){
        if(v!=fa){
        dfs(v,u);
        s[u]+=s[v]+1;
        }
    }
}

void Clear(){
    for(int i=1;i<=n;i++) g[i].clear();
}

main(){
    cin>>t;
    while(t--){
        int flag=0;
        memset(s,0,sizeof(s));
        cin>>n>>x;
        for(int i=0;i<n-1;i++){
            cin>>u>>v;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        dfs(x,-1);
        if(g[x].size()<=1) {puts("Ayush");Clear();continue;}
        for(int i:g[x]){
            if((s[i]+1)%2) {flag++;}
        }
        if(flag%2) puts("Ayush");
        else puts("Ashish");
        Clear();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章