poj 2912 Rochambeau(带权并查集)(枚举)

115 篇文章 0 订阅
20 篇文章 0 订阅

Rochambeau

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 M rounds. 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


思路:
很明显是关系并查集,他们之间的关系表达式也很容易推导出,但是裁判的确定却并不容易。

那这个时候我们观察到数据范围也不是很大,所以我们可以直接暴力枚举每一个人是否为裁判,枚举时对于涉及这个人的游戏回合都不进行并查集操作,看剩下的游戏中是否会有矛盾,如果有则说明这个人不是裁判并且此时要记录下在哪个回合出了矛盾,否则的话他就有可能是。

然后就遍历所有人看是否只有一个人没有出现矛盾,如果只有一个,则说明可以确定裁判,那么确定裁判的局数就是其他人出现矛盾的回合的最大值。因为只有否定了其他所有人,才能够确定才裁判。

代码:

#include<stdio.h>
#include<string.h>

#define maxn 500+10
#define maxv 2000+10
#define max(a,b) (a>b?a:b)
#define mem(a,b) memset(a,b,sizeof(a))

struct node
{
    int u,v,k;
} q[maxv];
int pre[maxn],rank[maxn],error[maxn];
int n,m;

void init()
{
    for(int i=0; i<=n; ++i)
        pre[i]=i,rank[i]=0;
}

int Find(int x)
{
    if(x==pre[x])
        return x;
    int temp=pre[x];
    pre[x]=Find(temp);
    rank[x]=(rank[x]+rank[temp])%3;
    return pre[x];
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int i,j,k,u,v;
        char op;
        for(i=1; i<=m; ++i)
        {
            scanf("%d %c %d",&q[i].u,&op,&q[i].v);
            if(op=='>')
                q[i].k=2;
            else if(op=='<')
                q[i].k=1;
            else if(op=='=')
                q[i].k=0;
        }
        mem(error,-1);
        for(i=0; i<n; ++i)
        {
            init();//注意这里要初始化,因为每次建立的关系都有可能不一样
            for(j=1; j<=m; ++j)
            {
                if(q[j].u==i||q[j].v==i)
                    continue;
                u=q[j].u,v=q[j].v,k=q[j].k;
                int fx=Find(u),fy=Find(v);
                if(fx!=fy)
                {
                    pre[fy]=fx;
                    rank[fy]=(rank[u]+k+3-rank[v])%3;
                }
                else
                {
                    if((rank[v]+3-rank[u])%3!=k)
                    {
                        error[i]=j;
                        break;
                    }
                }
            }
        }
        int cnt=0,ans=0;
        for(i=0; i<n; ++i)
        {
            if(error[i]==-1)
                ++cnt,v=i;
            ans=max(ans,error[i]);
        }
        if(!cnt)
            printf("Impossible\n");
        else if(cnt>1)
            printf("Can not determine\n");
        else
            printf("Player %d can be determined to be the judge after %d lines\n",v,ans);
    }
    return 0;
}



总结:
我的关系表达式是推出来了,但是裁判的确定却出了问题,我原先的想法是如果有哪个回合出了矛盾,就先判断下这个回合中的人是否已经被确定为裁判嫌疑人,如果不是那就把他们两个都设为嫌疑人,如果其中有一个人已经是嫌疑人,那么此时这个人就肯定是裁判了。如果没有嫌疑人,那就是Can not determine。如果同时出现了第三个和第四个嫌疑人,那么就肯定是impossible了。但是却wrong了0.0(不知道哪里错了)

好吧,数据小的时候要想到暴力枚举,不过并查集+枚举还是第一次做

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这个问题属于技术问题。以下是一个简单的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`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值