求助,WA0,悬一关
查看原帖
求助,WA0,悬一关
767496
RenSiYu6楼主2023/12/24 22:13
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5+10;
typedef pair<int,int>P;
int n,m,siz,color[maxn],vis[maxn],ans,f;
struct node{
    int op,v;
}a[maxn];
vector<P>G[maxn];
void dfs1(int x)
{
    siz++;vis[x]=1;
    for ( int i = 0;i<G[x].size();i++ )
    {
        int u = G[x][i].first;
        if ( !vis[u] )dfs1(u);
    }
}
void dfs2(int x)
{
    siz++;vis[x]=1;
    for ( int i = 0;i<G[x].size();i++ )
    {
        int u = G[x][i].first,val = G[x][i].second;
        if ( !vis[u] )
        {
            color[u] = (color[x]^val);
            dfs2(u);
        }
        else if (color[u]!=(color[x]^val))f =0;
    }
}
int main()
{
    int c,t;
    cin >> c >> t;
    while (  t--  )
    {
        cin >> n >> m;
        for ( int i = 1;i <= n;i++ )
        {
            G[i].clear();
        }
        memset(vis,0,sizeof(vis));
        ans =0;
        for ( int i = 1;i <= n;i++ ) a[i] = {1,i};
        for ( int i = 1;i <= m;i++ )
        {
            char ch;
            int x,y;
            cin >> ch;
            if ( ch == '+' )
            {
                cin >> x >> y;a[x]=a[y];
            }
            else if ( ch == '-' )
            {
                cin >> x >> y;;
                if ( a[y].op )a[x]={1,-a[y].v};
                else if ( a[y].v<=1 )a[x]={0,a[y].v^1};
                else a[x]={0,2};
            }
            else
            {
                cin >> x;
                if ( ch== 'T' )a[x] = {0,0};
                if ( ch== 'F' )a[x] = {0,0};
                if ( ch== 'U' )a[x] = {0,0};
            }
        }
        for ( int i = 1;i <= n;i++ )
        {
            if ( a[i].op )
            {
                int x,y;
                x = abs(a[i].v);
                if ( x==a[i].v )y=0;
                else
                {
                    y = 1;
                }
                G[i].push_back({x,y});
                G[x].push_back({i,y});
            }
        }
        for ( int i = 1;i <= n;i++ )
        {
            if ( !vis[i]&&a[i].op==0 )
            {
                siz=0;dfs1(i);
                if (a[i].v==2)ans+=siz;
            }
        }
        for ( int i = 1;i <= n;i++ )
        {
            if ( !vis[i] )
            {
                siz=0;f=1;color[i]=0;dfs2(i);
                if (!f)ans+=siz;
            }
        }
    }
    cout << ans << endl;
}
2023/12/24 22:13
加载中...