比赛 Asm_Def战记之透明计算网络 评测结果 AAAAAAAAAA
题目名称 Asm_Def三角形 最终得分 100
用户昵称 Asm.Def 运行时间 0.078 s
代码语言 C++ 内存使用 1.43 MiB
提交时间 2017-08-29 21:17:25
显示代码纯文本
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100005,mod = 998244353;
int f[maxn*2], N, M, Ans = 1, rt;
bool known[maxn*2], ans[maxn*2];
int fnd(int x){return f[x]==x?x:f[x]=fnd(f[x]);}
void init()
{
    for(int i = 1;i < maxn+maxn;++i)
        f[i] = i;
    scanf("%d%d", &N, &M);
    rt = 1;
    int op, x, y;
    while(M--)
    {
        scanf("%d%d%d", &op, &x, &y);
        if(!Ans || x == y) continue;
        if(x == rt || y == rt)
        {
            if(y == rt) swap(x, y);
            if(known[fnd(y)] && ans[fnd(y)] != op) Ans = 0;
            known[fnd(y)] = known[fnd(y+maxn)] = true;
            ans[fnd(y)] = op;
            ans[fnd(y+maxn)] = !op;
        }
        else
        {
            if(op)
            {
                if(fnd(x) == fnd(y)) Ans = 0;
                else
                {
                    if(known[fnd(x)] && known[fnd(y)])
                    {
                        if(ans[fnd(x)] == ans[fnd(y)]) Ans = 0;
                        continue;
                    }
                    else if(known[fnd(x)]) known[fnd(y)] = true, ans[fnd(y)] = !ans[fnd(x)];
                    else if(known[fnd(y)]) known[fnd(x)] = true, ans[fnd(x)] = !ans[fnd(y)];
                    f[fnd(x+maxn)] = fnd(y);
                    f[fnd(y+maxn)] = fnd(x);
                }
            }
            else
            {
                if(fnd(x+maxn) == fnd(y) || fnd(y+maxn) == fnd(x)) Ans = 0;
                else
                {
                    if(known[fnd(x)] && known[fnd(y)])
                    {
                        if(ans[fnd(x)] != ans[fnd(y)]) Ans = 0;
                        continue;
                    }
                    else if(known[fnd(x)])
                    {
                        known[y+maxn] = known[fnd(y)] = true;
                        ans[fnd(y)] = ans[fnd(x)];
                        ans[fnd(y+maxn)] = ans[fnd(x+maxn)];
                    }
                    else if(known[fnd(y)]) known[fnd(x)] = true, ans[fnd(x)] = ans[fnd(y)];
                    f[fnd(x)] = fnd(y);
                    f[fnd(x+maxn)] = fnd(y+maxn);
                }
            }
        }
    }
}
void work()
{
    if(Ans)
    {
        for(int i = 1;i <= N;++i) if(i != rt && !known[fnd(i)] && !known[fnd(i+maxn)])
        {
            Ans <<= 1;
            if(Ans >= mod) Ans -= mod;
            known[fnd(i)] = known[fnd(i+maxn)] = true;
        }
    }
    printf("%d\n", Ans);
}
int main()
{
    #ifdef DEBUG
    freopen("test.txt", "r", stdin);
    #else
    freopen("tria.in", "r", stdin);
    freopen("tria.out", "w", stdout);
    #endif // DEBUG
    init();
    work();
    return 0;
}