Timus - 1213 - Cockroaches!
先上题目:
1213. Cockroaches!
Memory limit: 64 MB
Input
Output
Sample
input | output |
---|---|
Gateway |
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!的更多相关文章
- LightOj 1213 - Fantasy of a Summation(推公式 快速幂)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1213 #include <stdio.h> int cases, case ...
- codeforces 719B:Anatoly and Cockroaches
Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...
- hdu 1213 How Many Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...
- 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行是 ...
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
- Codeforces 719B Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test:1 second memory limit per test:256 megabytes input:st ...
- Timus Online Judge:ural:1006. Square Frames
原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1006 看到题第一反应:这玩意怎么读入…… 本地的话因为是全角字符,会占两个位置,所以需要 ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- Timus 1132 Square Root(二次剩余)
http://acm.timus.ru/problem.aspx?space=1&num=1132 题意: 求 x^2 ≡ n mod p p是质数 的 解 本题中n>=1 特判p=2 ...
随机推荐
- [BZOJ 1741] Asteroids
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1741 [算法] 将每颗小行星的行,列相连,问题就转化为了求这张图的最小覆盖 由kon ...
- Python 35 进程间的通信(IPC机制)、生产者消费者模型
一:进程间的通信(IPC):先进先出 管道:队列=管道+锁 from multiprocessing import Queue q=Queue(4) q.put(['first',],block=T ...
- Java多线程技术-Lock/Condition
在java1.5中Lock对象来实现同步的效果,而且使用上更方便. 使用ReentrantLock实现同步 public class MyService { private Lock lock = n ...
- 基于CGAL的Delaunay三角网应用
目录 1. 背景 1.1 CGAL 1.2 cgal-bindings(Python包) 1.3 vtk-python 1.4 PyQt5 2. 功能设计 2.1 基本目标 2.2 待实现目标 3. ...
- ACM_Exponentiation
Exponentiation Time Limit: 2000/1000ms (Java/Others) Problem Description: Problems involving the com ...
- 06-联系人管理(xib应用)
ViewController.h文件中: @interface ViewController : UIViewController - (IBAction)add:(UIBarButtonItem * ...
- 网络开发之使用Web Service和使用WCF服务
判断是否有可用网络连接可以通过NetworkInterface类中的GetIsNetworkAvailable来实现: bool networkIsAvailable = networkInterfa ...
- 【java并发】(1)深入理解volatile关键字
volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...
- javascript跨域的几种方法
以下的例子包含的文件均为为 http://www.a.com/a.html .http://www.a.com/c.html 与 http://www.b.com/b.html,要做的都是从a.htm ...
- Android Activity has leaked window that was originally added
今天调试程序时log中突然打印这样的错误,但是程序并没有crash,为了不放过一个错误,我决定调查一下. 当时是离开一个activity,然后提示是否退出此界面,接下来就打印此错误: - ::): A ...