先上题目:

1213. Cockroaches!

Time limit: 1.0 second
Memory limit: 64 MB
  It's well-known that the most tenacious of life species on the Earth are cockroaches. They live everywhere if only there in food. And as far as they are unpretentious in food you can find them absolutely everywhere.
  A little Lyosha studies at school on a Space station. During one of the school competitions his class has reached the final. A task of the final contest is to exterminate all the cockroaches in the cargo module within minimal time.
  Within the long history of the competitions a unified tactics was worked out. The tactics is as follows: a poison gas is let in one of the module compartments and after that the baffle that separates the compartment from one of the adjacent ones is opened.  Cockroaches can't stand the smell of the gas and run to the other compartment. When there's no cockroaches in the treated compartment the baffle is closed. Afterwards analogously the next compartment is treated, and so on. The goal is to move all the cockroaches to the floodgate of the cargo module. Then the outward door is opened and all the cockroaches are engulfed by an open Space.
  Lyosha is responsible for programming the control board of the baffles in his team. The baffles are opened slowly, so it's very important to make do with minimal number of baffle openings in order to win in the contest. Your task is to help Lyosha to compute this number.

Input

  The first line contains a name of the floodgate compartment. Each of the next lines contains description of one of the baffles — the names of two compartments separated with a dash (-). The last line contains the only symbol "#". There are cockroaches in all the compartments of the module at first. It's possible to get to the floodgate from every compartment of the module passing several baffles. The total number of compartments doesn't exceed 30. The name of a compartment consists of no more than 20 Latin letters and digits. The large and the small letters should be distinguished.

Output

  Your program is to output the only number — the minimal amount of baffles that should be opened (and then closed) in order to move all the cockroaches to the floodgate.

Sample

input output
Gateway
Machinery-Gateway
Machinery-Control
Control-Central
Control-Engine
Central-Engine
Storage-Gateway
Storage-Waste
Central-Waste
#
6

  题意:给你一个终点的名字,给你一个连通图上不同点之间的关系,点上都有蟑螂。现在每一次可以选着一条边,将一个点上的蟑螂都赶到边的另一端,然后这条边会消失,问至少需要多少步操作才能将所有的蟑螂赶到终点。

  其实这就是在问一个联通图上面保留多少条边可以形成一棵树。树的结构是有n个点就有n-1条边。所以只需要记录每一个出现过的端点的名字各一次,树木减一就得出正确答案了。这里可以使用STL的set容器比较简单,当然用map也可以,不过set会快一点,也可以写一棵字典树,不过那样就很费时间了。

上代码:

 #include <cstdio>
#include <cstring>
#include <string>
#include <set> using namespace std; typedef struct{
char k[];
}st; set<string> s; int main()
{
st a,b,c;
string x,y;
int tot;
//freopen("data.txt","r",stdin);
scanf("%s",a.k);
x=a.k;
getchar();
tot=;
s.clear();
s.insert(x);
tot++;
while(scanf("%s",a.k),strcmp(a.k,"#")){
getchar();
int l=strlen(a.k);
int j=;
for(int i=;i<l;i++){
if(a.k[i]=='-'){
b.k[j]='\0';
strcpy(c.k,a.k+i+);
break;
}
b.k[j]=a.k[i];
j++;
}
x=b.k;
y=c.k;
if(s.count(x)<=){
s.insert(x);
tot++;
}
if(s.count(y)<=){
s.insert(y);
tot++;
}
}
printf("%d\n",tot-);
return ;
}

1213

Timus - 1213 - Cockroaches!的更多相关文章

  1. LightOj 1213 - Fantasy of a Summation(推公式 快速幂)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1213 #include <stdio.h> int cases, case ...

  2. codeforces 719B:Anatoly and Cockroaches

    Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...

  3. hdu 1213 How Many Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...

  4. Timus OJ 1997 Those are not the droids you're looking for (二分匹配)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1997 这个星球上有两种人,一种进酒吧至少玩a小时,另一种进酒吧最多玩b小时. 下面n行是 ...

  5. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

  6. Codeforces 719B Anatoly and Cockroaches

    B. Anatoly and Cockroaches time limit per test:1 second memory limit per test:256 megabytes input:st ...

  7. Timus Online Judge:ural:1006. Square Frames

    原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1006 看到题第一反应:这玩意怎么读入…… 本地的话因为是全角字符,会占两个位置,所以需要 ...

  8. HDU 1213 How Many Tables(模板——并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...

  9. Timus 1132 Square Root(二次剩余)

    http://acm.timus.ru/problem.aspx?space=1&num=1132 题意: 求 x^2 ≡ n mod p  p是质数 的 解 本题中n>=1 特判p=2 ...

随机推荐

  1. OTA制作及升级过程笔记【转】

    本文转载自:http://www.it610.com/article/5752570.htm 1.概述 1.1   文档概要 前段时间学习了AndroidRecovery模式及OTA升级过程,为加深理 ...

  2. thinkphp 日志记录

    日志记录\ThinkPHP\Lib\Think\Core\Log.class.php 1.可以在config.php中进行设置,默认为关闭状态. 'APP_DEBUG' => true 打开\T ...

  3. 手写DAO框架(一)-从“1”开始

    背景: 很久(4年)之前写了一个DAO框架-zxdata(https://github.com/shuimutong/zxdata),这是我写的第一个框架.因为没有使用文档,我现在如果要用的话,得从头 ...

  4. ROS-TF-新建坐标系

    前言:在前面的试验中,我们分别有wolrd,turtle1和turtle2三个坐标系,并且world是turtle1和turtle2的父坐标系.现在我们来新建一个自定义坐标系,让turtle2跟着新的 ...

  5. MySQL 的单表查询

    单表查询 语法: 一.单表查询的语法 SELECT 字段1,字段2 ,...FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY filed LIMIT ...

  6. font使用

    font连写属性 font-style  font-variant font-weight  font-size/line-height  font-family font-size与font-fam ...

  7. (转)Vue 爬坑之路(二)—— 组件之间的数据传递

    Vue 的组件作用域都是孤立的,不允许在子组件的模板内直接引用父组件的数据.必须使用特定的方法才能实现组件之间的数据传递. 首先用 vue-cli 创建一个项目,其中 App.vue 是父组件,com ...

  8. Java数组!!!你知多少?

    这是一篇网上搜到的文章,对数组进行了充分的说明.楼主懂了数组的俩种初始化方式:静态初始化和动态初始化方式.(贴过来备用哦) 下面我带大家一起加深一下对Java数组的认识: 1.理解数组 数组也是一种数 ...

  9. [2月1号] 努比亚全机型ROM贴 最全最新NubiaUI5.0 ROOT 极速体验

    前言 感谢在开发过程中mandfx和dgtl198312予以的帮助!本帖将整理所有Nubia手机的最新刷机包,还有少数机型未制作刷机包,需要的机油可以联系我制作recovery以及刷机包加群23722 ...

  10. 复习java基础第七天(反射)

    一:目标 Ø理解 Class 类 Ø理解 Java 的类加载机制 Ø学会使用 ClassLoader 进行类加载 Ø理解反射的机制 Ø掌握 Constructor.Method.Field 类的用法 ...