首页 > 其他分享 >uva 11082 A Plug for UNIX

uva 11082 A Plug for UNIX

时间:2023-04-13 20:44:57浏览次数:41  
标签:Plug int s2 len UNIX mp uva hd dis

 

#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <cstring>
using namespace std;
 const int N=1e4+2,M=5e5;
 
 int n1,n2,n3,len;
 map<string,int> mp;
 map<int,int> A,B;
 const int inf =1e9+7;
 int aa[402][402];  
 
 int all=1,hd[N],go[M],w[M],nxt[M];
   
 int S,T,n,m;
 int dis[M],ans=0,now[M];
   
 void add_(int x,int y,int z){
     nxt[++all]=hd[x]; hd[x]=all; go[all]=y;
     w[all]=z;
     swap(x,y);
     nxt[++all]=hd[x]; hd[x]=all; go[all]=y;
     w[all]=0;
 }
  bool bfs(){
     for(int i=0;i<M;i++)dis[i]=inf;
     queue<int> q;
     q.push(S);
     now[S]=hd[S];
     dis[S]=0;
       
     while(q.empty()==0){
         int x=q.front();
         q.pop();
         for(int i=hd[x];i;i=nxt[i]){
             int y=go[i];
             if(w[i]>0&&dis[y]==inf){
                 dis[y]=dis[x]+1;
                 now[y]=hd[y];
                 q.push(y);
                 if(y==T) return 1;
             }
         }
     }
     return 0;
 }
 int dfs(int x,int sum){
     if(x==T) return sum;
     int k,res=0;
       
     for(int i=now[x];i&& sum ;i=nxt[i]){
         now[x]=i;
         int y=go[i];
         if(w[i]>0&&(dis[y]==dis[x]+1)){
             k=dfs(y,min(sum,w[i]));
             if(k==0) dis[y]=inf;
             w[i]-=k;
             w[i^1]+=k;
             res+=k;
             sum-=k;
         }
     }
     return res;
 }
 void clear(){
	 memset(aa,0,sizeof aa);
 	 mp.clear(); A.clear(); B.clear();
 	 memset(hd,0,sizeof hd);all=1;
 }
 void sov(int cas){
 	int i,j ;
 	string s,s2;
 	
 	 clear();
 	 len=0;
 	 
 	 cin>>n1;
 	 for(i=1;i<=n1;i++){
 	 	 cin>>s;
 	 	 if(!mp.count(s)) mp[s]=++len;
 	 	 A[i]=mp[s];
	  }
	  cin>>n2; 
	 for(i=1;i<=n2;i++){
	 	cin>>s>>s2;
	 	if(!mp.count(s2)) mp[s2]=++len;
	 	B[i]=mp[s2];
	 }
	 cin>>n3;
	 
	 for(i=1;i<=n3;i++){
	 	 cin>>s>>s2;
	 	 if(!mp.count(s)) mp[s]=++len;
	 	 if(!mp.count(s2)) mp[s2]=++len;
		 aa[mp[s]][mp[s2]]=1;
	 }
	
	S=0,T=len+1;
	for(int k=1;k<=len;k++)
	 for(i=1;i<=len;i++)
	  for(j=1;j<=len;j++)
	   aa[i][j]|= aa[i][k]&&aa[k][j];
	   
	for(i=1;i<=len;i++)
	 for(j=1;j<=len;j++)
	  if(i!=j&&aa[i][j]) add_(i,j,inf);
	   
	for(i=1;i<=n1;i++) add_(A[i],T,1);
	for(i=1;i<=n2;i++) add_(S,B[i],1);
	
	int ans=0;
	while(bfs()) ans+=dfs(S,inf);
	cout<< n2-ans <<endl;        
	if(cas) cout<<endl;
 }
 signed main(){
  	int tes; cin>>tes;
  	while(tes--) sov(tes);
 } 
 
 

 

标签:Plug,int,s2,len,UNIX,mp,uva,hd,dis
From: https://www.cnblogs.com/towboa/p/17316342.html

相关文章

  • Calling Circles UVA - 247
    如果两个人相互打电话(直接或间接),则说他们在同一个电话圈里。例如,a打给b,b打给c,c打给d,d打给a,则这4个人在同一个圈里;如果e打给f但f不打给e,则不能推出e和f在同一个电话圈里。输入n(n≤25)个人的m次电话,找出所有电话圈。人名只包含字母,不超过25个字符,且不重复 对于一个有向图,Flo......
  • taro 3.0 官方模板运行报错 插件依赖 "@tarojs/plugin-platform-h5" 加载失败
    taroError:插件依赖"@tarojs/plugin-platform-h5"加载失败,请检查插件配置报错如下,原因:node版本的问题,使用nvm切换node版本就可以了......
  • 基于vue3+ts+vite的项目使用‘unplugin-auto-import/vite’插件,自动全局导入api的注意
    ​ 1.首先安装插件npminstallunplugin-auto-import@vitejs/plugin-vue-D2.安装完成后在vite.congfig.ts中配置,红色部分就是关于插件的基础自动导入部分,这样就可以将vue和router的相关api全局导入了,importAutoImportfrom"unplugin-auto-import/vite"exportdefaultde......
  • UVa 10049 Self-describing Sequence (自描述序列&二分递推)
    10049-Self-describingSequenceTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=990SolomonGolomb's self­describingsequence  istheonlynon­decreas......
  • Shanghai 2006 / UVa 1382 Distant Galaxy (枚举&扫描&动态维护)
    1382-DistantGalaxyTimelimit:3.000seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4128YouareobservingadistantgalaxyusingatelescopeabovetheAstronomyTower,......
  • UVa 514 Rails (water ver.)
    514-RailsTimelimit:3.000seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=455ThereisafamousrailwaystationinPopPushCity.Countrythereisincrediblyhilly.Thest......
  • UVa 11044 Searching for Nessy (water ver.)
    11044-SearchingforNessyTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=1985TheLochNessMonsterisamysteriousandunidentifiedanimalsaidtoinhabi......
  • UVa 846 Steps (数学)
    846-StepsTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=787Onestepsthroughintegerpointsofthestraightline.Thelengthofastepmustbenonnegat......
  • UVa 253 Cube painting (模拟)
    253-CubepaintingTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=189Wehaveamachineforpaintingcubes.Itissuppliedwiththreedifferentcolors:bl......
  • UVa 11498 Division of Nlogonia (water ver.)
    11498-DivisionofNlogoniaTimelimit:1.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2493TheProblemAftercenturiesofhostilitiesandskirmishesbetweenthefour......