POJ 2912 Rochambeau(枚举+并查集)

Rochambeau
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1575 Accepted: 540

Description

N children are playing Rochambeau (scissors-rock-cloth) game with you. One of them is the judge. The rest children are divided into three groups (it is possible that some group is empty). You don’t know who is the judge, or how the children are grouped. Then the children start playing Rochambeau game for Mrounds. Each round two children are arbitrarily selected to play Rochambeau for one once, and you will be told the outcome while not knowing which gesture the children presented. It is known that the children in the same group would present the same gesture (hence, two children in the same group always get draw when playing) and different groups for different gestures. The judge would present gesture randomly each time, hence no one knows what gesture the judge would present. Can you guess who is the judge after after the game ends? If you can, after how many rounds can you find out the judge at the earliest?

Input

Input contains multiple test cases. Each test case starts with two integers N and M (1 ≤ N ≤ 500, 0 ≤ M ≤ 2,000) in one line, which are the number of children and the number of rounds. Following are M lines, each line contains two integers in [0, N) separated by one symbol. The two integers are the IDs of the two children selected to play Rochambeau for this round. The symbol may be “=”, “>” or “<”, referring to a draw, that first child wins and that second child wins respectively.

Output

There is only one line for each test case. If the judge can be found, print the ID of the judge, and the least number of rounds after which the judge can be uniquely determined. If the judge can not be found, or the outcomes of the M rounds of game are inconsistent, print the corresponding message.

Sample Input

3 3
0<1
1<2
2<0
3 5
0<1
0>1
1<2
1>2
0<2
4 4
0<1
0>1
2<3
2>3
1 0

Sample Output

Can not determine
Player 1 can be determined to be the judge after 4 lines
Impossible
Player 0 can be determined to be the judge after 0 lines

Source

Baidu Star 2006 Preliminary 
Chen, Shixi (xreborner) living in http://fairyair.yeah.net/
 
 
 
 

枚举裁判,然后并查集判断

裁判由于可以任意出,所以可能属于任意一个集合,所以有裁判参与的会合不考虑,然后并查集部分和食物链很相似。

如果某个裁判那里出现了矛盾,则记录一下在哪出问题。

然后判断是否只有一个裁判没有出现问题。如果只有一个,说明可以确定,那么就是剩下的人出问题的最大值。因为只有否定了其它所有人,才能确定

 

 

/*
POJ 2912
枚举+并查集
枚举每一个裁判,看有没有不出错的
如果没有,说明是Impossible
如果有超过一个,那么就是Can not determine
如果只有一个,那么输出其他出错的位置的最大值
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN=510;
const int MAXM=2010;
struct Node
{
    int u,v;
    int re;
}node[MAXM];
int F[MAXN];
int val[MAXN];
int find(int x)
{
    if(F[x]==-1)return x;
    int tmp=find(F[x]);
    val[x]+=val[F[x]];
    val[x]%=3;
    return F[x]=tmp;
}
char str[30];
int main()
{
    int n,m;
    int u,v;
    while(scanf("%d%d",&n,&m)==2)
    {
        gets(str);
        for(int i=0;i<m;i++)
        {
            //scanf("%s",&str);
            gets(str);
            int t=0;
            int len=strlen(str);
            for(t=0;t<len;t++)
              if(str[t]=='>'||str[t]=='='||str[t]=='<')
                break;
            u=0;
            for(int j=0;j<t;j++)
            {
                u*=10;
                u+=str[j]-'0';
            }
            v=0;
            for(int j=t+1;j<len;j++)
            {
                v*=10;
                v+=str[j]-'0';
            }
            node[i].u=u;
            node[i].v=v;
            if(str[t]=='=')node[i].re=0;
            else if(str[t]=='<')node[i].re=1;
            else node[i].re=2;
        }
        int ansi;
        int anst=0;
        int t0=0;//不矛盾的个数
        for(int i=0;i<n;i++)
        {
            memset(F,-1,sizeof(F));
            memset(val,0,sizeof(val));
            int ff=-1;
            for(int j=0;j<m;j++)
            {
                if(node[j].u==i || node[j].v==i)continue;
                u=node[j].u;
                v=node[j].v;
                int t1=find(u);
                int t2=find(v);
                if(t1==t2)
                {
                    if(val[v]!=(val[u]+node[j].re)%3)
                    {
                        ff=j+1;
                        break;
                    }
                }
                else
                {
                    F[t2]=t1;
                    val[t2]=val[u]-val[v]+node[j].re;
                    val[t2]=(val[t2]+3)%3;
                }
            }
            if(ff==-1)
            {
                ansi=i;
                t0++;
            }
            else anst=max(anst,ff);
        }
        if(t0==0)printf("Impossible\n");
        else if(t0>=2)printf("Can not determine\n");
        else
           printf("Player %d can be determined to be the judge after %d lines\n",ansi,anst);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题属于技术问题。以下是一个简单的Python模拟登录POJ提交代码并抓取评测结果的代码示例: ```python import requests # 登录POJ,获取cookie def login(username, password): s = requests.Session() login_url = "http://poj.org/login" login_data = { "user_id1": username, "password1": password, "B1": "login", "url": "/" } s.post(login_url, data=login_data) return s # 提交代码 def submit_code(s, problem_id, language, source_code): submit_url = "http://poj.org/submit" submit_data = { "problem_id": problem_id, "language": language, "source": source_code } s.post(submit_url, data=submit_data) # 获取评测结果 def get_result(s, run_id): status_url = "http://poj.org/status" params = { "user_id": "", "result": "", "language": "", "top": run_id } r = s.get(status_url, params=params) table_start = r.text.find("<table cellpadding=0 cellspacing=0 border=0 width=100%>") table_end = r.text.find("</table>", table_start) table_html = r.text[table_start:table_end + 8] return table_html # 使用示例 username = "your_username" password = "your_password" problem_id = "1000" language = "G++" source_code = """ #include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; cout << a + b << endl; return 0; } """ s = login(username, password) submit_code(s, problem_id, language, source_code) table_html = get_result(s, "12345678") # 替换成实际提交的run id print(table_html) ``` 其中,`login`函数模拟登录POJ并返回一个`Session`对象,`submit_code`函数提交代码,`get_result`函数获取评测结果。你可以根据实际需要修改代码中的`username`、`password`、`problem_id`、`language`和`source_code`等参数,并替换`get_result`函数中的`run_id`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值