codeforces 141E clearing up

理解错题意了,自环应该全部不取

从中选出一些边组成一棵树,并使两种边的数量一样,自环取的话就不是一棵树了

题目给的边中除去自环的那些,对应到树的边有三种:

1.一定取M

2.一定取S

3.M或者S

随机地打印出(n-1)/2

所以要先构成一棵树,再慢慢调整



#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define N 1050
#define M 100500
int fa[N],used[M],u[M],v[M];
char k[M];
int n,m;
int fin(int u)
{
    return fa[u]==u ? u : fa[u]=fin(fa[u]);
}

int main ()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
        fa[i]=i;
    int num=0;
    for(int i=1;i<=m;++i)
    {
        scanf("%d%d %c",&u[i],&v[i],&k[i]);
        int x=fin(u[i]);
        int y=fin(v[i]);
        if(x!=y)
        {
            fa[x]=y;
            used[i]=1;
            if(k[i]=='S')
                num++;
        }
    }
    if(n==1)
    {
        printf("0\n");
        return 0;
    }
    if((n-1)%2)
    {
        printf("-1\n");
        return 0;
    }
    if(num*2+1!=n)
    {
        for(int i=1;i<=n;++i)
            fa[i]=i;
        if(num*2+1<n)
        {
            for(int i=1;i<=m;++i)
                if(used[i] && k[i]=='S')
                    fa[fin(u[i])]=fin(v[i]);
                else used[i]=0;
            for(int i=1;i<=m;++i)
                if(!used[i] && k[i]=='S')
            {
                int x=fin(u[i]);
                int y=fin(v[i]);
                if(x==y)
                    continue;
                fa[x]=y;
                used[i]=1;
                num++;
                if(2*num+1==n)
                    break;
            }
            for(int i=1;i<=m;++i)
                if(!used[i] && k[i]=='M')
            {
                int x=fin(u[i]);
                int y=fin(v[i]);
                if(x!=y)
                {
                    fa[x]=y;
                    used[i]=1;
                }
            }
        }
        else
        {
            for(int i=1;i<=m;++i)
                if(used[i] && k[i]=='M')
                    fa[fin(u[i])]=fin(v[i]);
                else used[i]=0;
            for(int i=1;i<=m;++i)
                if(!used[i] && k[i]=='M')
            {
                int x=fin(u[i]);
                int y=fin(v[i]);
                if(x==y)
                    continue;
                fa[x]=y;
                used[i]=1;
                num--;
                if(2*num+1==n)
                    break;
            }
            for(int i=1;i<=m;++i)
                if(!used[i] && k[i]=='S')
            {
                int x=fin(u[i]);
                int y=fin(v[i]);
                if(x!=y)
                {
                    fa[x]=y;
                    used[i]=1;
                }
            }
        }
    }
    if(2*num+1==n)
    {
        printf("%d\n",n-1);
        int z=0;
        for(int i=1;i<=m;++i)
           if(used[i])
           {
            printf("%d",i);
            z++;
            if(z<n-1)
                printf(" ");
            else printf("\n");
           }
    }
    else
        printf("-1\n");
    return 0;
}



你可能感兴趣的:(codeforces 141E clearing up)