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]
h1 h2 ... 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]
e11 e12
e21 e22
...
em1 em2
[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

最新文章

  1. Physics(物理系统)
  2. 玩转JavaScript OOP[2]&mdash;&mdash;类的实现
  3. Xshell远程连接工具
  4. js 淡入淡出的图片
  5. nginx的gzip选项和expire过期时间记录
  6. UVALive 6263 The Dragon and the knights --统计,直线分平面
  7. vim 安装与运行以及代码的运行
  8. ASP.NET导出数据到Excel 实例介绍
  9. UVALive 7457 Discrete Logarithm Problem (暴力枚举)
  10. jyphon 环境变量配置
  11. Java :构造器中的显式参数和this隐式参数
  12. Java学习笔记5---命令行下用javac,java编译运行含package语句的类
  13. vagrant三网详解(团队/个人开发必看) 转
  14. The 19th Zhejiang University Programming Contest - H
  15. PostgreSQL自学笔记:5 数据类型和运算符
  16. linux不同终端的操作是如何在messages日志中区分的
  17. 二、Jmeter脚本开发
  18. redis make编译失败的原因
  19. 洛谷P3768 简单的数学题
  20. POJ1742Coins(多重背包)

热门文章

  1. Android学习总结(三)——IntentService的用法
  2. maven打包错误:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
  3. SAP云平台,区块链,超级账本和智能合约
  4. HTML5与PHP的比较
  5. leetcode_1052. Grumpy Bookstore Owner
  6. mybatis 原理研究
  7. hash 散列表
  8. 解决IllegalBlockSizeException:last block incomplete in decryption异常
  9. syslog(),closelog()与openlog()--日志操作函数 (1)
  10. Greenplum/Deepgreen(集群/分布式)安装文档