Light OJ 1429 Assassin's Creed (ii) BFS + contraction + minimum path Overwrite

Source: Internet
Author: User

Source: Light OJ 1429 Assassin's Creed (II)

Question: at least a few people can go through the full graph and repeat the directed graph.

Ideas: if it is a DAG graph and each vertex cannot be repeated, it means that the bare minimum path overwrites the Dag. Currently, not the Dag may have loops, and each vertex may be repeated. For the graph with a ring, the point can be reduced. it is a DAG graph. In addition, we can repeat the same point as poj 2594 and pre-process the connectivity first.

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <stack>using namespace std;const int maxn = 1010;int vis[maxn];int y[maxn];vector <int> G[maxn], G2[maxn], G3[maxn];int n, m;int a[maxn][maxn];int pre[maxn];int low[maxn];int sccno[maxn];int dfs_clock;int scc_cnt;stack <int> S;void dfs(int u){pre[u] = low[u] = ++dfs_clock;S.push(u);for(int i = 0; i < G[u].size(); i++){int v = G[u][i];if(!pre[v]){dfs(v);low[u] = min(low[u], low[v]);}else if(!sccno[v])low[u] = min(low[u], pre[v]);}if(pre[u] == low[u]){scc_cnt++;while(1){int x = S.top();S.pop();sccno[x] = scc_cnt;if(x == u)break;}}}void find_scc(){dfs_clock = scc_cnt = 0;memset(sccno, 0, sizeof(sccno));memset(pre, 0, sizeof(pre));for(int i = 1; i <= n; i++)if(!pre[i])dfs(i);}void BFS(int u){queue <int> Q;memset(vis, 0, sizeof(vis));vis[u] = true;Q.push(u);while(!Q.empty()){int x = Q.front(); Q.pop();for(int i = 0; i < G2[x].size(); i++){int v = G2[x][i];if(vis[v])continue;vis[v] = true;G[u].push_back(v);Q.push(v);}}}bool dfs2(int u){    for(int i = 0; i < G3[u].size(); i++)    {        int v = G3[u][i];        if(vis[v])            continue;        vis[v] = true;        if(y[v] == -1 || dfs2(y[v]))        {            y[v] = u;            return true;        }    }    return false;}int match(){    int ans = 0;    memset(y, -1, sizeof(y));    for(int i = 1; i <= scc_cnt; i++)    {memset(vis, 0, sizeof(vis));if(dfs2(i))    ans++;    }    return ans;}int main(){int cas = 1;int T;scanf("%d", &T);while(T--){scanf("%d %d", &n, &m);for(int i = 0; i <= n; i++)G[i].clear(), G2[i].clear(), G3[i].clear();while(m--){int u, v;scanf("%d %d", &u, &v);G2[u].push_back(v);}for(int i = 1; i <= n; i++)BFS(i);find_scc();for(int u = 1; u <= n; u++){for(int i = 0; i < G[u].size(); i++){int v = G[u][i];if(sccno[u] != sccno[v])G3[sccno[u]].push_back(sccno[v]);}}printf("Case %d: %d\n", cas++, scc_cnt-match());}    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.