CF1704E Count Seconds
#include<bits/stdc++.h>
#define int long long
#define mod 998244353
using namespace std;
int T = 0;
int n = 0 , m = 0 , cnt = 0 , ans = 0;
array<int , 200100> head , val , add , ind , out;
struct Node{
int to;
int nxt;
};
array<Node , 4000100> edge;
queue<int> q;
void new_line(int a , int b)
{
edge[++ cnt].to = b;
edge[cnt].nxt = head[a];
head[a] = cnt;
return;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> T;
while(T --)
{
cin >> n >> m;
ans = 0;
int sum = 0;
fill(head.data() + 1 , head.data() + n + 1 , 0);
fill(add.data() + 1 , add.data() + n + 1 , 0);
fill(out.data() + 1 , out.data() + n + 1 , 0);
fill(val.data() + 1 , val.data() + n + 1 , 0);
fill(ind.data() + 1 , ind.data() + n + 1 , 0);
fill(edge.data() + 1 , edge.data() + m + 1 , Node{0 , 0});
for(int i = 1;i <= n;++ i)
{
cin >> val[i];
sum += val[i];
}
for(int i = 1;i <= m;++ i)
{
int x = 0 , y = 0;
cin >> x >> y;
new_line(x , y);
++ ind[y];
++ out[x];
}
if(sum == 0)
{
cout << "0" << endl;
continue;
}
for(int i = 1;i <= n;++ i)
{
bool check = 0;
fill(add.data() + 1 , add.data() + n + 1 , 0);
for(int x = 1;x <= n;++ x)
{
if(!val[x]) continue;
for(int e = head[x];e;e = edge[e].nxt)
{
int to = edge[e].to;
++ add[to];
}
}
for(int x = 1;x <= n;++ x)
{
if(val[x] > 0) -- val[x];
}
for(int x = 1;x <= n;++ x)
{
val[x] += add[x];
if(val[x] != 0)
{
check = 1;
}
}
if(!check)
{
ans = i;
break;
}
}
if(ans != 0)
{
cout << ans << endl;
continue;
}
for(int i = 1;i <= n;++ i)
{
val[i] %= mod;
}
for(int i = 1;i <= n;++ i)
{
if(!ind[i])
{
q.push(i);
}
}
int t = 0;
for(int i = 1;i <= n;++ i)
{
if(!out[i]) t = i;
}
while(!q.empty())
{
int x = q.front();
q.pop();
for(int e = head[x];e;e = edge[e].nxt)
{
int to = edge[e].to;
val[to] += val[x];
val[to] %= mod;
-- ind[to];
if(!ind[to]) q.push(to);
}
}
cout << (val[t] + n % mod) % mod << endl;
}
return 0;
}
CF1704E Count Seconds 为什么统计答案是valt+n 不是 maxval1→n+n