a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

HDU 3861 Tarjan + 縮點 + 最小路徑覆蓋

2019-11-14 10:01:05
字體:
供稿:網(wǎng)友

The King’s PRoblem

Time Limit: 2000/1000 MS (java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2685    Accepted Submission(s): 978Problem DescriptionIn the Kingdom of Silence, the king has a new problem. There are N cities in the kingdom and there are M directional roads between the cities. That means that if there is a road from u to v, you can only go from city u to city v, but can’t go from city v to city u. In order to rule his kingdom more effectively, the king want to divide his kingdom into several states, and each city must belong to exactly one state. What’s more, for each pair of city (u, v), if there is one way to go from u to v and go from v to u, (u, v) have to belong to a same state. And the king must insure that in each state we can ether go from u to v or go from v to u between every pair of cities (u, v) without passing any city which belongs to other state.  Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into. InputThe first line contains a single integer T, the number of test cases. And then followed T cases. The first line for each case contains two integers n, m(0 < n <= 5000,0 <= m <= 100000), the number of cities and roads in the kingdom. The next m lines each contains two integers u and v (1 <= u, v <= n), indicating that there is a road going from city u to city v. OutputThe output should contain T lines. For each test case you should just output an integer which is the least number of states the king have to divide into. Sample Input
13 21 21 3 Sample Output
2 題意是將一些點劃分區(qū)域,同時有兩個規(guī)定:1.若有u,v兩個點,u->v且v->u 即n,v兩點可以互相到達形成環(huán),則一定分在同一區(qū)域思路:Tarjan求強連通分量然后縮點。2.在同一區(qū)域的任意兩點至少存在一條路徑可以相互到達,即(設(shè)同一區(qū)域兩點u,v)有u->v或 v->u。思路:二分圖,很明顯是最小路徑覆蓋,縮點后建新圖,跑個匈牙利得到最大匹配 ans,結(jié)果就為: 縮點后的點數(shù) num 減去 ans。代碼:
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int INF = 1e8;const int maxn = 5010;vector<int> G[maxn],G2[maxn];int low[maxn],dfn[maxn]; int vis[maxn],instack[maxn],point[maxn],match[maxn];int n,tot,num;stack<int> S;void init(void){    tot = num = 0;    for(int i=0 ;i<=n ;i++){        G[i].clear();        G2[i].clear();        match[i] = -1;        low[i] = dfn[i] = 0;        vis[i] = instack[i] = point[i] = 0;    }    while(S.size())    S.pop();}void Tarjan(int x){    low[x] = dfn[x] = tot++;    vis[x] = instack[x] = 1;    S.push(x);    for(int i=0 ;i<G[x].size();i++){        int v = G[x][i];        if(!vis[v]){            Tarjan(v);            low[x] = min(low[x],low[v]);        }        else if(instack[v]){            low[x] = min(low[x],dfn[v]);        }    }    if(low[x] == dfn[x]){        while(1){            int t = S.top();            S.pop();            instack[t] = 0;            point[t] = num;             if(t == x)    break;        }        num++;    }}bool find(int x){	for(int i=0 ;i<G2[x].size() ;i++){		int t = G2[x][i];		if(!vis[t]){			vis[t] = 1;			if(match[t] == -1 || find(match[t])){				match[t] = x;				return true;			}		}		}	return false;}int main(){   	int T;   	scanf("%d",&T);   	while(T--){		int m;		scanf("%d%d",&n,&m);		init();   		while(m--){   			int x,y;			scanf("%d%d",&x,&y);			G[x].push_back(y);			}		for(int i=1 ;i<=n ;i++){			if(!vis[i]){				Tarjan(i);			}		}		for(int i=1 ;i<=n ;i++){			for(int j=0 ;j<G[i].size() ;j++){				if(point[i] != point[G[i][j]]){					G2[point[i]].push_back(point[G[i][j]]);				}			}		}		int ans = 0;		for(int i=0 ;i<num ;i++){			memset(vis,0,sizeof(vis));			if(find(i))				ans++;		}		cout << num-ans << endl;			}    return 0;}  
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 天堂在线一区二区 | 国产精品日韩欧美一区二区 | 日韩一级在线免费观看 | 老司机午夜免费精品视频 | 国产精品美女视频 | 精品自拍网 | 超碰在线网 | 自拍第一页 | 日韩欧美中文字幕在线观看 | 国精日本亚洲欧州国产中文久久 | 精品国产乱码久久久久久蜜臀 | 日韩视频一区二区三区 | 青青久久 | 中文字幕一区二区三区四区五区 | 中国一级免费毛片 | 爱爱视频天天看 | 日韩成人免费 | 亚洲精品视频在线 | 欧美三日本三级三级在线播放 | 日韩在线欧美 | 久久久久久久久久久久久久av | 色老头在线观看 | 91电影在线观看 | 国产一区二区三区91 | 中文日韩在线 | 日韩精品免费在线 | 国产成人精品一区二区视频免费 | 国产小视频在线观看 | 日韩国产精品视频 | 久久婷婷色 | 在线免费观看黄 | 成人免费毛片高清视频 | 精品免费视频 | 日韩色| 日本免费电影一区 | 日韩国产在线 | 美女久久 | 成人黄网在线观看 | 青青久在线视频免费观看 | 久久亚洲国产精品 | 高清不卡一区 |