A Bug's Life -Poj

原文地址:Bug's Life -Poj 作者:兰多夫87
A Bug's Life
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 25599 Accepted: 8332
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



主要算法:并查集
我们用f[i]表示i的父亲,用v[i]表示i与他父亲的关系,0表示同性,1表示异性
首先我们初始化,f[i]=i;v[i]=0;
每次给出两只虫子,我们先判断他们的祖先是否相同,如果相同,那他们的关系我们是已知的,可以通过他们与祖先的关系推出,再判断是否矛盾
如果不相同,就把它们的祖先的关系推出来,再把他们的祖先建立关系
注意路径压缩时,v[x]一起更新,不要弄错了


代码:
const
  maxn=2000;
var
  f,v:array[0..maxn]of longint;
  t,i:longint;

function find(x:longint):longint;
var
  k:longint;
begin
  if f[x]=x then exit(x);
  k:=f[x];
  f[x]:=find(f[x]);
  v[x]:=(v[x]+v[k])and 1;
  exit(f[x]);
end;

procedure work;
var
  n,m,i,j,k,x,y:longint;
begin
  fillchar(f,sizeof(f),0);
  fillchar(v,sizeof(v),0);
  readln(n,m);
  for i:=1 to n do
    f[i]:=i;
  for i:=1 to m do
    begin
      readln(x,y);
      if find(x)=find(y) then
      if (v[x]+v[y])and 1<>1 then
      begin
        writeln('Suspicious bugs found!');
        for j:=i+1 to m do
          readln;
        exit;
      end;
      k:=v[x];
      x:=f[x];
      f[x]:=f[y];
      v[x]:=((k+v[y])+1)and 1;
    end;
  writeln('No suspicious bugs found!');
end;

begin
  readln(t);
  for i:=1 to t do
    begin
      writeln('Scenario #',i,':');
      work;
      writeln;
    end;
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值