Air RaidTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7218 Accepted Submission(s): 4800 Problem Description 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.
Input 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:
Output 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.
Sample Input 2 4 3 3 4 1 3 2 3 3 3 1 3 1 2 2 3
Sample Output 2 1
Source
Recommend Ignatius.L |
给出m条边,求最小的交叉点使得可以覆盖所有的边。
最小路径覆盖问题。
样例二
#include <algorithm> //STL通用算法
#include <bitset> //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex> //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque> //STL双端队列容器
#include <exception> //异常处理类
#include <fstream>
#include <functional> //STL定义运算函数(代替运算符)
#include <limits>
#include <list> //STL线性列表容器
#include <map> //STL 映射容器
#include <iomanip>
#include <ios> //基本输入/输出支持
#include<iosfwd> //输入/输出系统使用的前置声明
#include <iostream>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <queue> //STL队列容器
#include <set> //STL 集合容器
#include <sstream> //基于字符串的流
#include <stack> //STL堆栈容器
#include <stdexcept> //标准异常类
#include <streambuf> //底层输入/输出支持
#include <string> //字符串类
#include <utility> //STL通用模板类
#include <vector> //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 2500+6;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
struct node
{
int to;
int nxt;
} e[maxn*maxn];
struct Two
{
int head[maxn*2];
int dep[maxn];
int dx[maxn];
int dy[maxn];
int mx[maxn];
int my[maxn];
int n,m,cnt;
void init(int nn)
{
cnt=0;
n=nn;
memset(head,-1,sizeof(head));
}
void add_edge(int u,int v)
{
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
int bfs()
{
int flag=0;
queue<int>q;
for(int i=0; i<=n+1; i++)
{
dx[i]=dy[i]=-1;
}
for(int i=1; i<=n; i++)
if(mx[i]==-1)
q.push(i);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int k=head[u]; k!=-1; k=e[k].nxt)
{
int v=e[k].to;
if(dy[v]==-1)
{
dy[v]=dx[u]+1;
if(my[v]==-1)
flag=1;
else
{
dx[my[v]]=dy[v]+1;
q.push(my[v]);
}
}
}
}
return flag;
}
bool dfs(int u)
{
for(int k=head[u]; k!=-1; k=e[k].nxt)
{
int v=e[k].to;
if(dy[v]==dx[u]+1)
{
dy[v]=-1;
if(my[v]==-1||dfs(my[v]))
{
my[v]=u;
mx[u]=v;
return 1;
}
}
}
return 0;
}
void solve()
{
for(int i=0; i<=n+1; i++)
{
mx[i]=my[i]=-1;
}
int ans = 0;
while(bfs())
{
for(int i = 1; i <= n; ++i)
if(mx[i] == -1 && dfs(i))
ans++;
}
printf("%d\n",n-ans);
}
} D;
int main()
{
int n,t,m;
int cnt=0;
scanf("%d",&t);
while(t--)///bool
{
//printf("Scenario #%d:\n",++cnt);
scanf("%d %d",&n,&m);
D.init(n);
for(int i=1; i<=m; i++)
{
int u,v;
scanf("%d %d",&u,&v);
D.add_edge(u,v);
}
D.solve();
}
return 0;
}