POJ-1904 King's Quest 强连通分量求完美匹配

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 4105;
const int inf = 1<<29;
int n;
int times,ans,top;
bool vis[maxn];
int low[maxn],dfn[maxn],stack[maxn],p[maxn];
vector<int>map[maxn];
vector<int>cnt;

void dfs(int u)
{
int v;
dfn[u] = low[u] = ++times;                     //为节点u设定次序
vis[u] = true;                             //判断是否在栈中
stack[++top] = u;							  //将节点i压入栈中
for (  int i = 0; i < map[u].size(); i ++ )						 //遍历每一条边
{
v = map[u][i];
if ( !dfn[v] )					//如果节点i未被访问过
{
dfs(v);					 //继续向下查找
low[u] = low[v] <= low[u]?low[v]:low[u];
}
else if ( vis[v] ) //如果节点i在栈中
low[u] = dfn[v] <= low[u]?dfn[v]:low[u];
}
if ( dfn[u] == low[u] )     //如果节点i是强连通分量的根
{
ans++;                          //强连通分量数
do
{
v = stack[top--];             //把栈里v上面的顶点弹栈
p[v] = ans;
vis[v] = false;
}while ( v != u );

}
}
void tarjan()
{
ans = times = top = 0;                //初始化
memset( vis,0,sizeof(vis) );
memset( dfn,0,sizeof(dfn) );
for( int i = 1; i <= n; i ++ )         //遍历每个节点
if ( !dfn[i] )
dfs(i);
}
int main()
{
//freopen("data.txt","r",stdin);
int m,temp,g;
while( scanf("%d",&m) != EOF )
{
for( int i = 1; i <= 2*m; i ++ )
map[i].clear();
for( int i = 1; i <= m; i ++ )
{
scanf("%d",&temp);
for( int j = 1; j <= temp; j ++ )
{
scanf("%d",&g);
map[i].push_back(g+m);
}
}
for( int i = 1; i <= m; i ++ )
{
scanf("%d",&g);
map[g+m].push_back(i);
}
n = m*2;
tarjan();
for( int u = 1; u <= m; u ++ )
{
cnt.clear();
for( int i = 0; i < map[u].size(); i ++ )
{
int v = map[u][i];
if( p[u] == p[v] )
cnt.push_back(v-m);
}
printf("%d",cnt.size());
sort( cnt.begin(),cnt.end() );
for( int j = 0; j < cnt.size(); j ++ )
printf(" %d",cnt[j]);
puts("");
}
}
return 0;
}

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 5105;
const int inf = 1<<29;
int n;
int times,ans,top;
bool map[maxn][maxn],vis[maxn];
int low[maxn],dfn[maxn],stack[maxn],p[maxn];
void dfs(int u)
{
dfn[u] = low[u] = ++times;                     //为节点u设定次序
vis[u] = true;                             //判断是否在栈中
stack[++top] = u;							  //将节点i压入栈中
for ( int i = 1; i <= n; i ++ )						  //遍历每一条边
{
if( map[u][i] )
{
if (!dfn[i])					//如果节点i未被访问过
{
dfs(i);					 //继续向下查找
low[u] = low[i] <= low[u]?low[i]:low[u];
}
else if ( vis[i] ) //如果节点i在栈中
low[u] = dfn[i] <= low[u]?dfn[i]:low[u];
}
}
int i;
if ( dfn[u] == low[u] )     //如果节点i是强连通分量的根
{
ans++;                          //强连通分量数
do
{
i = stack[top--];             //把栈里v上面的顶点弹栈
p[i] = ans;
vis[i] = false;
}while ( i != u );

}
}
void tarjan()
{
int i;
ans = times = top = 0;                //初始化
memset( vis,0,sizeof(vis) );
memset( dfn,0,sizeof(dfn) );
for( i = 1; i <= n; i ++ )         //遍历每个节点
if ( !dfn[i] )
dfs(i);
}
int main()
{
//freopen("data.txt","r",stdin);
int m,temp,g;
while( scanf("%d",&m) != EOF )
{
memset(map,0,sizeof(map));
for( int i = 1; i <= m; i ++ )
{
scanf("%d",&temp);
for( int j = 1; j <= temp; j ++ )
{
scanf("%d",&g);
map[i][m+g] = true;
}
}
for( int i = 1; i <= m; i ++ )
{
scanf("%d",&g);
map[m+g][i] = true;
}
n = m*2;
tarjan();
vector<int>ans;
for( int i = 1; i <= m; i ++ )
{
ans.clear();
for( int j = m+1; j <= n; j ++ )
if( map[i][j] && p[i] == p[j] )
ans.push_back(j-m);
printf("%d",ans.size());
for( int j = 0; j < ans.size(); j ++ )
printf(" %d",ans[j]);
puts("");
}
}
return 0;
}

【poj】1904 King's Quest【强连通】

2016-08-06 02:14:16

POJ-1904:King's Quest

2018-07-16 21:03:07

POJ-1904 King's Quest

2015-09-08 22:25:03

【连通图|二分匹配+强连通分量】POJ-1904 King's Quest

2015-03-11 16:21:44

poj1904 - King's Quest

2012-09-15 16:44:10

PKU1904 king's quest

2009-08-22 21:47:00

POJ 1904 King's Quest（强连通分量+匹配）

2015-09-22 00:21:00

poj1904 King's Quest 强连通分量

2015-01-20 11:23:14

POJ 1904 King's Quest强连通分量+二分图完美匹配

2016-05-10 11:11:14

PKU 1904 King's Quest

2009年10月20日 1018B 下载