tjut 4687

#include<cstdio>  
#include<cstring>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<list>  
#include<cmath>  
#include<set>  
#include<queue>  
using namespace std;  
const int maxn = 55;  
int n,m,Match[maxn],Head,Tail,Queue[maxn],Start,Finish,NewBase,Father[maxn],Base[maxn],cnt,Count;  
bool InQueue[maxn],InPath[maxn],InBlossom[maxn],Graph[maxn][maxn];  
void Push(int u) {  
    Queue[Tail] = u;  
    Tail++;  
    InQueue[u] = true;  
}  
int Pop() {  
    int res = Queue[Head];  
    Head++;  
    return res;  
}  
int FindCommonAncestor(int u,int v) {  
    memset(InPath,false,sizeof(InPath));  
    while(true) {  
        u = Base[u];  
        InPath[u] = true;  
        if(u == Start) break;  
        u = Father[Match[u]];  
    }  
    while(true) {  
        v = Base[v];  
        if(InPath[v])break;  
        v = Father[Match[v]];  
    }  
    return v;  
}  
void ResetTrace(int u) {  
    int v;  
    while(Base[u] != NewBase) {  
        v = Match[u];  
        InBlossom[Base[u]] = InBlossom[Base[v]] = true;  
        u = Father[v];  
        if(Base[u] != NewBase) Father[u] = v;  
    }  
}  
void BloosomContract(int u,int v) {  
    NewBase = FindCommonAncestor(u,v);  
    memset(InBlossom,false,sizeof(InBlossom));  
    ResetTrace(u);  
    ResetTrace(v);  
    if(Base[u] != NewBase) Father[u] = v;  
    if(Base[v] != NewBase) Father[v] = u;  
    for(int tu = 1; tu <= n; tu++)  
        if(InBlossom[Base[tu]]) {  
            Base[tu] = NewBase;  
            if(!InQueue[tu]) Push(tu);  
        }  
}  
void FindAugmentingPath() {  
    memset(InQueue,false,sizeof(InQueue));  
    memset(Father,0,sizeof(Father));  
    for(int i = 1; i <= n; i++)  
        Base[i] = i;  
    Head = Tail = 1;  
    Push(Start);  
    Finish = 0;  
    while(Head < Tail) {  
        int u = Pop();  
        for(int v = 1; v <= n; v++)  
            if(Graph[u][v] && (Base[u] != Base[v]) && (Match[u] != v)) {  
                if((v == Start) || ((Match[v] > 0) && Father[Match[v]] > 0))  
                    BloosomContract(u,v);  
                else if(Father[v] == 0) {  
                    Father[v] = u;  
                    if(Match[v] > 0)  
                        Push(Match[v]);  
                    else {  
                        Finish = v;  
                        return;  
                    }  
                }  
            }  
    }  
}  
void AugmentPath() {  
    int u,v,w;  
    u = Finish;  
    while(u > 0) {  
        v = Father[u];  
        w = Match[v];  
        Match[v] = u;  
        Match[u] = v;  
        u = w;  
    }  
}  
void Edmonds() {  
    memset(Match,0,sizeof(Match));  
    for(int u = 1; u <= n; u++)  
        if(Match[u] == 0) {  
            Start = u;  
            FindAugmentingPath();  
            if(Finish > 0)AugmentPath();  
        }  
}  
int getMatch() {  
    Edmonds();  
    Count = 0;  
    for(int u = 1; u <= n; u++)  
        if(Match[u] > 0)  
            Count++;  
    return Count/2;  
}  
struct node {  
    int a,b;  
    node(int a=0,int b=0): a(a),b(b) {}  
    bool operator < (const node& rhs) const {  
        return a < rhs.a || (a == rhs.a && b < rhs.b);  
    }  
    bool operator == (const node& rhs)const {  
        return a == rhs.a && b == rhs.b;  
    }  
} e[155],ee[155];  
map<node,int> p;  
void PrintMatch() {  
    cnt = getMatch();  
vector<int> ans;  
    int g[maxn][maxn];  
    memcpy(g,Graph,sizeof(g));  
    for(int i=0;i<m;i++) {  
        memcpy(Graph,g,sizeof(g));  
        int u = e[i].a, v = e[i].b;  
        for(int j = 1; j <= n; j++)  
                Graph[j][u] = Graph[u][j] = Graph[j][v] = Graph[v][j] = false;  
        int cur = getMatch();  
        if(cur == cnt - 1) ;  
        else ans.push_back(i+1);  
    }  
    int sz = ans.size();  
        printf("%d\n",sz);  
        for(int i = 0;i < sz;i++)  
        {  
            printf("%d",ans[i]);  
            if(i < sz-1)printf(" ");  
        }  
        printf("\n");  
}  
int main() {  
    while(~scanf("%d%d",&n,&m)) {  
        memset(Graph,0,sizeof(Graph));  
        for(int i=0; i<m; i++) {  
            scanf("%d%d",&e[i].a,&e[i].b);  
            Graph[e[i].a][e[i].b]=Graph[e[i].b][e[i].a]=true;  
        }  
        Edmonds();  
        PrintMatch();  
    }  
    return 0;  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值