POJ 2492 A Bug's Life 带权并查集!

Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it. Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
Sample Input
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Output
Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!
Hint
Huge input,scanf is recommended.
哎,卡了一天了,差一点就跟着别人的思路走了!
这道题跟网上大多数题目的思路是有区别的,这是一种带权并查集,而网络上的思路大多是分组并查集
说一下思路,并查集就是关系的区分,如果可以交配,这两个虫肯定有关系,所以我们把他们分到一组
但是他们又有各自的性别,所以权值就是他们的性别。
0代表不可以自己交配,所以一开始的关系都初始为0
        for(int i=0; i<=n; i++)
        {
            pre[i]=i;
            rale[i]=0;
        }
之后就是关系的连接,如果虫子之间不是同性恋,那么每个相邻节点就是0101010101之类的,所以我们根据上一个节点就可以推知下一个节点是个什么东西
但是!由于我们路径压缩,我们回破坏父亲节点的关系,所以有如下关系就可以求出父亲节点到当前节点的关系。
int findboss(int x)
{

    if(x==pre[x])
        return x;
    else
    {
        int k=pre[x];
         pre[x]=findboss(pre[x]);
        rale[x]=(rale[x]^rale[k]);
        return pre[x];
    }
}
下面看一看合并,当连个点的boss节点不同的时候,我们需要对节点进行连接,我发现,当两边与头节点关系不同的时候,两个头节点关系相同,反之则不同,所以再用异或处理一下
            if(x!=y)
            {
                pre[x]=y;//并到y的集合内
                rale[x]=(!(rale[u]^rale[v]));
            }
下面就是判断了,很简单,当出于同一并查集的时候,如果两个当前点与boss节点的关系是一样的,这说明他们性别一样,而他们之间又有一条边,产生矛盾,问题解决。  下面ac代码
#include<stdio.h>
using namespace std;
int pre[2333];
int rale[2333];
int findboss(int x)
{

    if(x==pre[x])
        return x;
    else
    {
        int k=pre[x];
         pre[x]=findboss(pre[x]);
        rale[x]=(rale[x]^rale[k]);
        //printf("%d\n",rale[x]);
        return pre[x];
    }
}
int main()
{

    int t;
    scanf("%d",&t);
    int kase=1;
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0; i<=n; i++)
        {
            pre[i]=i;
            rale[i]=0;
        }
        int flag=1;
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            int x=findboss(u);
            int y=findboss(v);
            if(x!=y)
            {
                pre[x]=y;//并到y的集合内
                rale[x]=(!(rale[u]^rale[v]));
            }
            else
            {
                if(rale[u]==rale[v])
                {
                    flag=0;
                }
            }

        }
       // for(int i=1;i<=n;i++)
           // printf("%d ",rale[i]);
        printf("Scenario #%d:\n",kase++);
        if(flag)
        {
            printf("No suspicious bugs found!\n");
        }
        else
        {
            printf("Suspicious bugs found!\n");
        }
        if(t)
            printf("\n");

    }
}



  • 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、付费专栏及课程。

余额充值