【32.70%】【poj 2492】A Bug's Life
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 34687 Accepted: 11344
Description
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.
Source
TUD Programming Contest 2005, Darmstadt, Germany
【题解】
每个测试点之间要多输出一行空行。
因为有多组数据,所以不能看到有抵触关系就break掉。而应该继续读完这组数据。不然可能我们输入的东西还是上组数据的。。
相当于给你m句话。
x和y不是同一性别。
问有没有假话。
在并查集里面加一维r代表该节点和父亲节点的关系。
如果为0表示性别相同。为1表示性别不同。
可以得到以下两个带权向量关系
t就是r[x]+r[y];
t再%2;
然后路径压缩的时候就可以靠这个来获取儿子和爷爷的关系了。
然后是合并两个集合。
设x的根节点为a,y的根节点为b;
此时并查集的高度为2(经过路径压缩);
我们执行的时候是执行f[b] = a;
即
x和y的关系知道。但是b和a的关系不知道啊;
我们仍可用向量关系退出a和b的关系;
可以看到1+r[x] == t+r[y];
那么t = 1+[rx]-r[y];
t要再%2;
这样就求出a和b的关系t了;
最后是判断一句话是否为真的方法;
如果f[x]==f[y]则判断x和y是不是异性;
用向量的运算,可以得到x和y的关系就是r[x]-r[y]
这里r[x]-r[y]可能会小于0,所以加上2再%2;
#include <cstdio>
const int MAXN = 2500;
int n, m;
int f[MAXN], relation[MAXN];
void input(int &r)
{
char t;
t = getchar();
while (t<'0' || t>'9')
t = getchar();
r = 0;
while (t >= '0' && t <= '9')
{
r = r * 10 + t - '0';
t = getchar();
}
}
int findfather(int x)
{
if (f[x] == x)
return x;
int olfa = f[x];
f[x] = findfather(f[x]);
relation[x] = (relation[x] + relation[olfa]) % 2;
return f[x];
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
int T;
input(T);
for (int TT = 1; TT <= T; TT++)
{
input(n); input(m);
for (int i = 1; i <= n; i++)
f[i] = i, relation[i] = 0;
bool wrong = false;
for (int i = 1; i <= m; i++)
{
int x, y;
input(x); input(y);
if (wrong)
continue;
int a = findfather(x), b = findfather(y);
if (a != b)
{
f[b] = a;
relation[b] = (1 + relation[x] - relation[y])%2;
}
else
{
int temp = (relation[x] - relation[y] + 2) % 2;
if (temp != 1)
wrong = true;
}
}
printf("Scenario #%d:\n", TT);
if (wrong)
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n");
}
return 0;
}
【32.70%】【poj 2492】A Bug's Life的更多相关文章
- 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)
题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...
- 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)
题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...
- 【poj 1988】Cube Stacking(图论--带权并查集)
题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【POJ】【2348】Euclid‘s Game
博弈论 题解:http://blog.sina.com.cn/s/blog_7cb4384d0100qs7f.html 感觉本题关键是要想到[当a-b>b时先手必胜],后面的就只跟奇偶性有关了 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
随机推荐
- 【2017 Multi-University Training Contest - Team 2】 Regular polygon
[Link]: [Description] 给你n个点整数点; 问你这n个点,能够组成多少个正多边形 [Solution] 整点只能构成正四边形. 则先把所有的边预处理出来; 枚举每某两条边为对角线的 ...
- 洛谷 P1358 扑克牌
P1358 扑克牌 题目描述 组合数学是数学的重要组成部分,是一门研究离散对象的科学,它主要研究满足一定条件的组态(也称组合模型)的存在.计数以及构造等方面的问题.组合数学的主要内容有组合计数.组合设 ...
- 【直接拿来用のandroid公共代码模块解析与分享】の Notification和NotificationManager
本文源代码托管在https://github.com/ASCE1885/asce-common,欢迎fork Android项目做得多了.会发现原来非常多基础的东西都是能够复用,这个系列介绍一些自己项 ...
- [LuoguP4892]GodFly的寻宝之旅 状压DP
链接 基础状压DP,预处理出sum,按照题意模拟即可 复杂度 \(O(n^22^n)\) #include<bits/stdc++.h> #define REP(i,a,b) for(in ...
- 带你一分钟理解闭包--js面向对象编程(转载他人)
什么是闭包? 先看一段代码: function a(){ var n = 0; function inc() { n++; console.log(n); } inc(); inc(); } a(); ...
- [ Eclipse ] [ Problem ] Eclipse 無法開啟問題
因為 Eclipse 在設定環境的過程掛掉太多次,擷取一些網路上優秀的文章當作備份 http://www.ewdna.com/2013/12/Eclipse-Loading-Workbench.htm ...
- IntelliJ IDEA 启动tomcat 报错: idea Unable to open debugger port (127.0.0.1:58233): java.net.SocketException "socket closed"
debug启动项目弹出提示 Error running omp: Unable to open debugger port (127.0.0.1:50812): java.net.SocketExce ...
- win7旗舰版怎么降级到专业版
一.操作准备及注意事项 1.UltraISO光盘制作工具9.5 2.备份C盘及桌面文件 二.win7旗舰版改成专业版的步骤 1.当前系统为Win7 SP1 64位旗舰版: 2.按Win+R打开运行,输 ...
- 洛谷 P1130 红牌
P1130 红牌 题目描述 某地临时居民想获得长期居住权就必须申请拿到红牌.获得红牌的过程是相当复杂 ,一共包括N个步骤.每一步骤都由政府的某个工作人员负责检查你所提交的材料是否符合条件.为了加快进程 ...
- 使用knockout.js 完毕template binding
//1.template <script id="txn-details-template" type="text/html"> <!--St ...