The citizens of a small village are tired of being the only inhabitants around without a connection to the Internet. After nominating the future network administrator, his house was connected to the global network. All users that want to have access to the Internet must be connected directly to the admin's house by a single cable (every cable may run underground along streets only, from the admin's house to the user's house). Since the newly appointed administrator wants to have everything under control, he demands that cables of different colors should be used. Moreover, to make troubleshooting easier, he requires that no two cables of the same color go along one stretch of street.
Your goal is to find the minimum number of cable colors that must be used in order to connect every willing person to the Internet.
Input
t [the number of test cases, t<=500]
n m k [n <=500 the number of houses (the index of the admin's house is 1)]
[m the number of streets, k the number of houses to connect]
h1h2 ... hk [a list of k houses wanting to be conected to the network, 2<=hi<=n]
[The next m lines contain pairs of house numbers describing street ends]
e11e12
e21e22
...
em1em2
[next cases]
Output
For each test case print the minimal number of cable colors necessary to make all the required connections.
Example
Input:
2
5 5 4
2 3 4 5
1 2
1 3
2 3
2 4
3 5
8 8 3
4 5 7
1 2
1 8
8 7
1 3
3 6
3 2
2 4
2 5 Output:
2
1
Warning: large Input/Output data, be careful with certain languages
二分路径上出现过的最多种的颜色,然后网络流跑一跑看看能不能到达所有点
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,cnt,S,T;
int c[];
struct edge{
int to,next,v;
}e[];
struct ed{int x,y;}d[];
int head[];
int cur[];
inline void ins(int u,int v,int w)
{
e[++cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
e[cnt].v=w;
}
inline void insert(int u,int v,int w)
{
ins(u,v,w);
ins(v,u,);
}
int h[];
int q[];
int ans;
inline bool bfs()
{
for (int i=;i<=T;i++)h[i]=-;
int t=,w=;
q[]=S;h[S]=;
while (t!=w)
{
int now=q[t++];
for(int i=head[now];i;i=e[i].next)
if (e[i].v&&h[e[i].to]==-)
{
h[e[i].to]=h[now]+;
q[w++]=e[i].to;
}
}
return h[T]!=-;
}
inline int dfs(int x,int f)
{
if (x==T||!f)return f;
int w,used=;
for (int i=head[x];i;i=e[i].next)
if (e[i].v&&h[e[i].to]==h[x]+)
{
w=dfs(e[i].to,min(e[i].v,f-used));
e[i].v-=w;
e[i^].v+=w;
used+=w;
if (f==used)return f;
}
if (!used)h[x]=-;
return used;
}
inline void rebuild(int mid)
{
for (int i=;i<=T;i++)head[i]=;
S=;T=n+;cnt=;
for (int i=;i<=k;i++)insert(c[i],T,);
for (int i=;i<=m;i++)
{
insert(d[i].x,d[i].y,mid);
insert(d[i].y,d[i].x,mid);
}
}
inline bool jud(int mid)
{
rebuild(mid);
ans=;while (bfs())ans+=dfs(S,inf);
return ans==k;
}
inline void work()
{
n=read();m=read();k=read();
for (int i=;i<=k;i++)c[i]=read();
for (int i=;i<=m;i++)
{
d[i].x=read();
d[i].y=read();
}
int l=,r=k,col=k;
while (l<=r)
{
int mid=(l+r)>>;
if (jud(mid))col=mid,r=mid-;
else l=mid+;
}
printf("%d\n",col);
}
int main()
{
int T=read();
while (T--)work();
}
Spoj NETADMIN