QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#212156#4620. If You Can't Beat Them, Join Them!yiyiyiAC ✓541ms160764kbC++172.6kb2023-10-13 10:04:032023-10-13 10:04:03

Judging History

你现在查看的是最新测评结果

  • [2023-10-13 10:04:03]
  • 评测
  • 测评结果:AC
  • 用时:541ms
  • 内存:160764kb
  • [2023-10-13 10:04:03]
  • 提交

answer

#include <bits/stdc++.h>
#define lowbit(x) x&(-x)
#define mp make_pair
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
#define forE(i,x) for(int i=head[x];i;i=nxt[i])
#define pii pair<int,int>
#define fi first
#define se second
#define int long long
using namespace std;
const int INF=1e9+7;
const int mod=998244353;
const int maxn=4e6+5;
const int maxm=4e6+5;
inline int read()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+(c-'0');
		c=getchar();
	}
	return x*f;
}
int k,n,m,s;
vector<int> e[maxn];
int cnt,ver[maxm],nxt[maxm],head[maxn];
inline void add(int x,int y)
{
    cnt++;
    ver[cnt]=y;
    nxt[cnt]=head[x];
    head[x]=cnt;
}


int sg[maxn],step[maxn];
int po[maxn],in[maxn],vis[maxn];
pii b[maxn];
inline void init()
{
    cnt=0;
    rep(i,0,n) head[i]=0,e[i].clear();
    rep(i,0,n) sg[i]=step[i]=in[i]=vis[i]=0;
}
inline void solve()
{
    int k=read();
    rep(i,1,k)
    {
        init();
        n=read(),m=read(),s=read();
        rep(j,1,m) {int x=read(),y=read();add(x,y);e[y].push_back(x);in[x]++;}
        queue<int> q;
        rep(j,1,n) sg[j] = -1;
        rep(j,1,n) if(!in[j]) sg[j]=0,q.push(j);
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(auto y:e[x])
            {
                if(sg[x] == 0)
                {
                    sg[y] = 1;
                    in[y] = 0;
                    if(!step[y]) step[y]=step[x]+1;
                    step[y] = min(step[y],step[x]+1);
                    if(!vis[y]) vis[y] = 1,q.push(y);
                }
                else 
                {
                    if(vis[y]) continue;
                    in[y]--;
                    if(in[y] == 0)
                    {
                        for(int j=head[y];j;j=nxt[j]) step[y] = max(step[y],step[ver[j]]+1);  
                        sg[y] = 0;
                        vis[y] = 1;
                        q.push(y);
                    }
                }
            }
        }
        if(sg[s]==-1) step[s] = INF;
        b[i]=mp(step[s],sg[s]);

    }
    sort(b+1,b+k+1);
    int ans=0;
    rep(i,1,k)
    {
        if(b[i].second==0) continue;
        if(b[i].first>20000000) break;
        ans+=po[k-i];
        ans%=mod;
    }
    printf("%lld\n",ans);
}

signed main()
{
    po[0]=1;
    rep(i,1,2000000) po[i]=po[i-1]*2%mod;
    int T=read();
    while(T--)
    {
        solve();
    }
}

详细

Test #1:

score: 100
Accepted
time: 541ms
memory: 160764kb

input:

5
1000
100 200 35
1 2
1 12
1 14
1 15
1 55
1 100
2 3
2 5
2 11
2 23
2 39
2 60
2 79
2 18
3 4
3 7
3 16
3 18
3 31
3 44
3 20
4 6
4 10
4 63
5 8
5 49
5 88
5 22
5 52
6 25
6 89
6 29
6 17
7 17
7 27
7 85
8 9
8 20
8 54
8 42
8 94
9 13
9 80
9 27
10 27
10 47
10 70
10 81
10 74
10 60
11 19
11 84
11 63
12 21
12 50
12 ...

output:

325998728
792
235
509
776469527

result:

ok 5 lines