hdu acm 1151 Air Raid
2016-02-03 13:45
519 查看
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4322 Accepted Submission(s): 2875
[align=left]Problem Description[/align]
Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection
i.e. the town's streets form no cycles.
With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper
lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.
[align=left]Input[/align]
Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format:
no_of_intersections
no_of_streets
S1 E1
S2 E2
......
Sno_of_streets Eno_of_streets
The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets
in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk
<= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.
There are no blank lines between consecutive sets of data. Input data are correct.
[align=left]Output[/align]
The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections
in the town.
[align=left]Sample Input[/align]
2
4
3
3 4
1 3
2 3
3
3
1 3
1 2
2 3
[align=left]Sample Output[/align]
2
1
题目意思:最少派多少个伞兵沿着街道走完所有的路口(有向图,不可以回头)
解题关键:最小路径覆盖=顶点数-最大匹配数。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#include<vector>
#define maxn 200
#define MS(a,b) memset(a,b,sizeof(a));
int head[maxn],link[maxn],vis[maxn],f;
struct node
{
int e,next;
}edge[50005];
void add(int s,int e)
{
edge[f].e=e;
edge[f].next=head[s];
head[s]=f++;
}
bool dfs(int u)
{
int i,v;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].e;
if(!vis[v])
{
vis[v]=1;
if(link[v]==-1||dfs(link[v]))
{
link[v]=u;
return true;
}
}
}
return false;
}
int main()
{
int t,n,m,i,a,b,ans;
cin>>t;
while(t--)
{
cin>>n;
cin>>m;
f=0;
MS(head,-1);
for(i=0;i<m;i++)
{
cin>>a>>b;
add(a,b);
}
MS(link,-1);
ans=0;
for(i=1;i<=n;i++)
{
MS(vis,0);
if(dfs(i))
ans++;
}
cout<<n-ans<<endl;
}
return 0;
}
Total Submission(s): 4322 Accepted Submission(s): 2875
[align=left]Problem Description[/align]
Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection
i.e. the town's streets form no cycles.
With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper
lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.
[align=left]Input[/align]
Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format:
no_of_intersections
no_of_streets
S1 E1
S2 E2
......
Sno_of_streets Eno_of_streets
The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets
in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk
<= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.
There are no blank lines between consecutive sets of data. Input data are correct.
[align=left]Output[/align]
The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections
in the town.
[align=left]Sample Input[/align]
2
4
3
3 4
1 3
2 3
3
3
1 3
1 2
2 3
[align=left]Sample Output[/align]
2
1
题目意思:最少派多少个伞兵沿着街道走完所有的路口(有向图,不可以回头)
解题关键:最小路径覆盖=顶点数-最大匹配数。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#include<vector>
#define maxn 200
#define MS(a,b) memset(a,b,sizeof(a));
int head[maxn],link[maxn],vis[maxn],f;
struct node
{
int e,next;
}edge[50005];
void add(int s,int e)
{
edge[f].e=e;
edge[f].next=head[s];
head[s]=f++;
}
bool dfs(int u)
{
int i,v;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].e;
if(!vis[v])
{
vis[v]=1;
if(link[v]==-1||dfs(link[v]))
{
link[v]=u;
return true;
}
}
}
return false;
}
int main()
{
int t,n,m,i,a,b,ans;
cin>>t;
while(t--)
{
cin>>n;
cin>>m;
f=0;
MS(head,-1);
for(i=0;i<m;i++)
{
cin>>a>>b;
add(a,b);
}
MS(link,-1);
ans=0;
for(i=1;i<=n;i++)
{
MS(vis,0);
if(dfs(i))
ans++;
}
cout<<n-ans<<endl;
}
return 0;
}
相关文章推荐
- 一个基于MVVM的TableView组件化实现方案——AITableView
- 手表端开发遇到的错误 E/WearablePkgInstaller: Failed to add com.. 和Ignoring Google Play Services dependent app:
- [LeetCode]217. Contains Duplicate
- df 命令结果显示的 Size 不等于 Avail + Used
- poj3250Bad Hair Day【单调栈】
- UVA 11374 Airport Express
- HDU 1021 Fibonacci Again 数学题
- .net 4.5 async await 简单用法
- 个人电脑上搭建openstack的实验室http://geek.csdn.net/news/detail/53890
- 4320: ShangHai2006 Homework 并查集+离线处理 思路题
- error: command 'gcc' failed with exit status 1
- 4320: ShangHai2006 Homework|并查集
- 位图和矢量图区别,PS和AI的区别
- CDOJ 1270 Playfair
- 在Word 2010中,为粘贴无格式文本设置快捷键 Alt+V
- Spark PairRDD 转化二
- Light OJ 1038 Race to 1 Again (期望DP)
- poj2585 Window Pains(拓扑排序判定)
- 编译spark1.6.0出现 Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:exec (sparkr-pkg)
- 五子棋AI算法第二篇-极大极小值搜索算法