Codeforces C. Game On Leaves (貪心 / “博弈”) (Round #646 Div.2)

傳送門

題意: 給你一個n個節點的無根樹和一個特殊節點x。Ayush和Ashish輪流在樹上進行遊戲:找到一個葉節點(度大於或等於零)將其刪除(包括以其爲端點的邊),刪除特殊點的就是贏家,且Ayush爲先手。輸出每個測試的贏家名字。
在這裏插入圖片描述
思路: 這道題看起來是博弈,其實就是個貪心的思維題

  • 若特殊點就是葉節點則直接先手Ayush贏
  • 將特殊點看做根節點,必須刪除其他n - 2個點後再看誰是先手誰就是贏家(也就相當於n % 2 == 0則最後一局依舊是Ayush先手,否則就是Ashish贏)。

代碼實現:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 1010;

int t, n, x;
int d[N];

signed main()
{
    //IOS;

    cin >> t;
    while(t --){
        me(d);
        cin >> n >> x;
        //先統計每個節點的度
        for(int i = 1; i < n; i ++){
            int l, r;
            cin >> l >> r;
            d[l] ++; d[r] ++;
        }
        //如果特殊點就是葉節點
        if(!d[x] || d[x] == 1) puts("Ayush");
        else{
            if((n - 2) % 2) puts("Ashish");
            else puts("Ayush");
        }
    }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章