ACM-ICPC 2019 山东省省赛D Game on a Graph
阅读原文时间:2023年07月16日阅读:1

Game on a Graph

Time Limit: 1 Second Memory Limit: 65536 KB

There are people playing a game on a connected undirected simple graph with () vertices (numbered from 0 to ) and edges. These people, numbered from 0 to , are divided into two groups and the game goes as follows:

They take turns to make the move. That is to say, person number 0 will make the 1st move, person number 1 will make the 2nd move, …, person number will make the -th move.

During a move, the current player MUST select an edge from the current graph and remove it. If the graph is no longer connected after removing the edge, the group this person belongs to loses the game (and of course their opponents win), and the game ends immediately.

Given the initial graph when the game starts, if all people use the best strategy to win the game for their groups, which group will win the game?

Recall that a simple graph is a graph with no self loops or multiple edges.

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer (), indicating the number of people.

The second line contains a string of length (). indicates that person number belongs to the 1st group, and indicates that person number belongs to the 2nd group.

The third line contains two integers and (, ), indicating the number of vertices and edges of the initial graph.

The following lines each contains two integers and (), indicating that there is an edge connecting vertex and in the initial graph.

It’s guaranteed that:

The initial graph is a connected undirected simple graph.

There exist two people who belong to different groups.

The sum of , the sum of and the sum of in all test cases will not exceed .

Output

For each test case output one line containing one integer. If the 1st group wins, output “1” (without quotes); If the 2nd group wins, output “2” (without quotes).

Sample Input

3

5

11212

4 6

0 1

0 2

0 3

1 2

1 3

2 3

5

11121

5 7

0 2

1 3

2 4

0 3

1 2

3 2

4 1

3

121

4 3

0 1

0 2

1 3

Sample Output

2

1

2

考的图的性质,N个定点,最少需要N-1条边构成了联通图。有人说是博弈论,反正我没看出来

一开始以为是无向图,打算往后放放,突然队友告诉我读错题了,尴尬!?

#include <bits/stdc++.h>
using namespace  std;
char ob[100005];
int main()
{
    int t,n, x,y,z;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=0;i<n;i++) cin>>ob[i];
        cin>>x>>y;
        for(int i=0;i<y;i++) cin>>z>>z;
        y=(y-x+1)%n;
        if(ob[y]=='1')cout<<2<<endl;
        else cout<<1<<endl;
    }
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器