不想说什么了,一道大水题,早上脑子烧了去调其他题,这道题用朴素dfs水过去,以为能过完,结果gg.
用二分图匈牙利算法和网络流都能过.
题目大意:http://www.lydsy.com/JudgeOnline/problem.php?id=1433
我们可以把在这个学校里的人作为二分图左侧(因为他们都不走,都占床位),二分图右侧就是所有的床位(外校生本来没有自己的床位),由于只睡与自己认识的人的床位,所以就把左侧的人向右侧自己能睡的床位建边(注意本校生(且不离开),自己要向自己原来的床位建边)。最后跑一遍二分图匈牙利算法,有一人找不床位就gg.网络流同理,只需在二分图两侧建一个原点和汇点,原点向二分图左边建容量为1的边,汇点向右边建容量为1的边,跑一边dinic就可,最好看一下最大流有没有等于在校人数(即左侧二分图的数量).
**备注:
右侧节点要+n,防止出现二分图左边连向左边的情况(重复).**
匈牙利
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
int id[505],n,T,oth[505],match[505],top,num,x,h[505];
bool vis[505],mark;
inline const int read(){
register int f=1,x=0;
register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
struct edge{
int nxt,v;
}e[5005];
void add(int u,int v){
e[++num].v=v;
e[num].nxt=h[u];
h[u]=num;
}
bool find(int u){
for(int i=h[u];i;i=e[i].nxt){
int v=e[i].v;
if(!vis[v]){
vis[v]=true;
if(match[v]==-1||find(match[v])){
match[v]=u;
return true;
}
}
}
return false;
}
int main(){
T=read();
while(T--){
top=0,num=0,mark=true;
memset(h,0,sizeof(h));
memset(match,-1,sizeof(match));
n=read();
for(int i=1;i<=n;i++) id[i]=read();
for(int i=1;i<=n;i++){
x=read();
if(x==1&&id[i]==1) {id[i]=2;continue;}
oth[++top]=i;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
x=read();
if(x==1){
if(id[i]==1&&id[j]==2) add(i,j+n),add(j+n,i);
if(id[i]==1&&id[j]==1) add(i,j+n),add(j+n,i);
if(id[i]==0&&id[j]!=0) add(i,j+n),add(j+n,i);
}
if(x==0&&i==j&&id[i]==1) add(i,j+n),add(j+n,i);
}
for(int i=1;i<=top;i++){
memset(vis,false,sizeof(vis));
if(!find(oth[i])) {mark=false;break;}
}
if(mark) printf("^.^\n");
else printf("T.T\n");
}
}
网络流
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#define INF 0x7fffffff
using namespace std;
const int N = 205;
int last[N],S,T,n,h[N],q[N],cnt=1,cur[N],sign[N],ans,MT,inclass[N],dis[N];
struct edge{
int nxt,v,c;
}e[5005];
void insert( int u, int v, int w ){
e[++cnt].v = v; e[cnt].nxt = h[u]; e[cnt].c = w; h[u] = cnt;
e[++cnt].v = u; e[cnt].nxt = h[v]; e[cnt].c = 0; h[v] = cnt;
}
bool bfs(){
memset(dis,-1,sizeof(dis));
dis[S]=0;
queue<int> q;
q.push(S);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=h[u];i;i=e[i].nxt){
int v=e[i].v;
if(dis[v]==-1&&e[i].c){
dis[v]=dis[u]+1;
q.push(v);
}
}
}
if(dis[T]==-1) return false;
return true;
}
int dfs(int u,int a){
if(u==T) return a;
int f,flow=0,x;
for(int i=cur[u];i;i=e[i].nxt){
int v=e[i].v;
if(dis[v]==dis[u]+1){
x=min(e[i].c,a-flow);
f=dfs(v,x);
e[i].c-=f; e[i^1].c+=f; flow+=f;
if(e[i].c) cur[u]=i;
if(flow == a) return flow;
}
}
if(!flow) dis[u]=-1;
return flow;
}
void dinic(){
while( bfs() ){
for( int i = S; i <= T; i++ ) cur[i] = h[i];
ans += dfs(S,INF);
}
}
int main(){
scanf("%d", &MT);
while( MT-- ){
scanf("%d", &n); S = 0; T = n*2+1; int yu = n; ans = 0;
cnt = 1; memset(h,0,sizeof(h)); memset(sign,0,sizeof(sign));
for( int i = 1; i <= n; i++ ) scanf("%d", &inclass[i]);
for( int i = 1,x; i <= n; i++ ){ scanf("%d", &x); if( inclass[i] && x == 1 ) sign[i] = 1; }
for( int i = 1; i <= n; i++ ) if( !sign[i] ) insert(S,i,1);
for( int i = 1; i <= n; i++ ) if( inclass[i] ) insert(i+n,T,1);
for( int i = 1; i <= n; i++ )
for( int j = 1,x; j <= n; j++ ){
scanf("%d",&x);
if( sign[i] || !inclass[j] ) continue;
if( x || i == j ) insert(i,j+n,1);
}
for( int i = 1; i <= n; i++ ) if( sign[i] ) yu--;
dinic();
if( ans == yu ) puts("^.^");
else puts("T.T");
}
return 0;
}
If someone loves a flower, of which just one single blossom grows in all the millions and millions of stars, it is enough to make him happy just to look at the stars. He can say to himself, “Somewhere, my flower is there…” But if the sheep eats the flower, in one moment all his stars will be darkened… And you think that is not important!