UVAlive 3523 Knights of the Round Table [点双连通分量] [Tarjan]

来源:互联网 发布:sql语言分为几种类型 编辑:程序博客网 时间:2024/06/05 19:17

Knights of the Round Table
Time Limit: 4500MS 64bit IO Format: %lld & %llu

Description
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights.
There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country.
Knights can easily get over-excited during discussions–especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized
that the fights can only be prevented if the knights are seated according to the following two rules:
• The knights should be seated such that two knights who hate each other should not be neighbors at the table. (Merlin has a list that says who hates whom.) The knights are sitting around a roundtable, thus every knight has exactly two neighbors.
• An odd number of knights should sit around the table. This ensures that if the knights cannot agree on something, then they can settle the issue by voting. (If the number of knights is even, then itcan happen that “yes” and “no” have the same number of votes, and the argument goes
on.)
Merlin will let the knights sit down only if these two rules are satisfied, otherwise he cancels the meeting. (If only one knight shows up, then the meeting is canceled as well, as one person cannot sit around a table.) Merlin realized that this means that there can be knights who cannot be part of any seating arrangements that respect these rules, and these knights will never be able to sit at the Round Table (one such case is if a knight hates every other knight, but there are many other possible reasons).
If a knight cannot sit at the Round Table, then he cannot be a member of the Knights of the Round Table and must be expelled from the order. These knights have to be transferred to a less-prestigious order, such as the Knights of the Square Table, the Knights of the Octagonal Table, or the Knights of the Banana-Shaped Table. To help Merlin, you have to write a program that will determine the number of knights that must be expelled.

Input
The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000. The number n is the number of knights. The next m lines describe which knight hates which knight. Each of these m lines contains two integers k1 and k2, which means that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1 and n).
The input is terminated by a block with n = m = 0.

Output
For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled.

Sample Input
5 5
1 4
1 5
2 5
3 4
4 5
0 0
Sample Output
2 1


时隔半年再写圆桌骑士,优化了许多地方。。。
首先这道题求不能参加任何会议的其实个数,那么就是不属于任何奇环的骑士个数。。
由于求出一个点双连通分量之后可能包含有若干个奇数环,对于没有对二分图判定作出贡献的那些点来说,由于属于同一个BCC,那么它也一定可以属于一个奇数环(可以分类讨论)。。。
所以只需要求出所有的BCC然后二分图判定,更新答案即可。。
注意color数组清零一定要在每一次染色之前!!!!!!
另外只需要入栈一条边即可,不用每次入栈,因为不是求出所有BCC的边。。

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 1005;struct Edge{    int to,next;}edge[maxn*maxn];int head[maxn];int maxedge;inline void addedge(int u,int v){    edge[++maxedge] = (Edge) { v,head[u] };    head[u] = maxedge;    edge[++maxedge] = (Edge) { u,head[v] };    head[v] = maxedge;}bool g[maxn][maxn];int n,m;inline bool init(){    if(!~scanf("%d%d",&n,&m) || (!n && !m)) return false;    memset(head,-1,sizeof(head));    maxedge=-1;    memset(g,0,sizeof(g));    for(int i=1;i<=m;i++)    {        int x,y;        scanf("%d%d",&x,&y);        g[x][y]=g[y][x]=true;    }    for(int i=1;i<n;i++)        for(int j=i+1;j<=n;j++)            if(!g[i][j]) addedge(i,j);    return true;}struct Road{    int u,v;    bool operator == (const Road t) const    {        return u==t.u && v==t.v;    }};stack <Road> sta;int bccno[maxn],dfn[maxn];int dfs_clock,bcc_cnt;struct Node{    int u,next;}node[maxn*maxn]; // limit dataint bcc[maxn],size[maxn];int maxnode;inline void addnode(int u){    node[++maxnode] = (Node) { u,bcc[bcc_cnt] };    bcc[bcc_cnt] = maxnode;    size[bcc_cnt]++;}inline void update(Road tmp){    if(bccno[tmp.u]!=bcc_cnt) bccno[tmp.u]=bcc_cnt,addnode(tmp.u);    if(bccno[tmp.v]!=bcc_cnt) bccno[tmp.v]=bcc_cnt,addnode(tmp.v);}int dfs(int u,int father){    int lowu=dfn[u]=++dfs_clock;    for(int i=head[u];~i;i=edge[i].next)    {        int v = edge[i].to;        Road e = (Road) { u,v };        if(!dfn[v])        {            sta.push(e);            int lowv = dfs(v,u);            smin(lowu,lowv);            if(lowv >= dfn[u])            {                bcc_cnt++;                Road tmp;                do                {                    tmp = sta.top(); sta.pop();                    update(tmp);                }while(!(tmp==e));            }        }        else if(dfn[v]<dfn[u] && v^father) smin(lowu,dfn[v]); // do not continue above but judge here!!!     }    return lowu;}void Tarjan(){    memset(bccno,0,sizeof(bccno));    memset(dfn,0,sizeof(dfn));    memset(bcc,-1,sizeof(bcc));    memset(size,0,sizeof(size));    maxnode=-1;    dfs_clock=0; bcc_cnt=0;    for(int i=1;i<=n;i++) if(!dfn[i])        dfs(i,-1);}int color[maxn];bool ans[maxn];bool bipartite(int u,int k) // u is partited already!{    for(int i=head[u];~i;i=edge[i].next)    {        int v = edge[i].to;        if(bccno[v]^k) continue;        if(color[u]==color[v]) return false;        if(!color[v])        {            color[v] = 3 - color[u];            if(!bipartite(v,k)) return false;        }    }    return true;}int work(){    memset(ans,0,sizeof(ans));    for(int k=1;k<=bcc_cnt;k++)    {        if(size[k]<3) continue;        for(int i=bcc[k];~i;i=node[i].next) bccno[node[i].u]=k;        memset(color,0,sizeof(color)); // initialize here every time before partiting!!!!        color[node[bcc[k]].u]=1;        if(!bipartite(node[bcc[k]].u,k))            for(int i=bcc[k];~i;i=node[i].next) ans[node[i].u]=true;    }    int ret = 0;    for(int i=1;i<=n;i++) if(!ans[i]) ret++;    return ret;}int main(){    #ifndef ONLINE_JUDGE    freopen("knights.in","r",stdin);    freopen("knights.out","w",stdout);    #endif    while(init())    {        Tarjan();        printf("%d\n",work());    }    return 0;}
0 0
原创粉丝点击