首页 > ACM题库 > HDU-杭电 > hdu 2627 Life-并查集-[解题报告]C++
2014
02-12

hdu 2627 Life-并查集-[解题报告]C++

Life

问题描述 :

Samuel,Former Zua leader,was arrested and had to stay in prison during the Spring Festival. He was in custody for money laundering and other cases.
According to the report,Samuelwas alleged to have embezzled 104 million New Zua dollars ($3.15 million) in public funds, accepted bribes of about $9 million in a land purchase deal, and taken a kickback of $2.7 million from a construction project.
Although Samuel is a smart lawyer,he was still arrested due to his illegal actions.
Now Samuel has a question for you:If you are given the legislations of Zua,can you tell how many years he stay in the prison?
The legislations of Zua are supposed as the following few lines:

输入:

Input will contains 3 parts. There is an integer t in the first line meaning t test cases. Then the next line has 2 parts:n(represents the cases Samuel was accused with),s(a string with the length of n,only contains “0”,”1”.Here ‘0’means he was not accused with this case,and ‘1’means he was accused with this case ) .The next n lines have the numbers of New Zua dollars(the unit is million) he was accused with.

输出:

Input will contains 3 parts. There is an integer t in the first line meaning t test cases. Then the next line has 2 parts:n(represents the cases Samuel was accused with),s(a string with the length of n,only contains “0”,”1”.Here ‘0’means he was not accused with this case,and ‘1’means he was accused with this case ) .The next n lines have the numbers of New Zua dollars(the unit is million) he was accused with.

样例输入:

2
3
011
10 5 15
5
01101
1 10 10 100 31 

样例输出:

Samuel was accused with 2 case(s),and he will stay in the prison for 5 year(s).
Samuel was accused with 3 case(s),and he will stay in the prison for 20 year(s).

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.


解题转自:http://blog.sina.com.cn/s/blog_a825ada90101picj.html


  1. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.

  2. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。

  3. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }