社区讨论
求助(关于神秘问题)
P6691选择题参与者 4已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @mhj3ymfy
- 此快照首次捕获于
- 2025/11/03 20:20 4 个月前
- 此快照最后确认于
- 2025/11/03 20:20 4 个月前
这份代码仅仅使用了1e+6大小的数组,但是maxN=1e+6时无法通过#20
而maxN=2e+6时却可以
求教原因谢谢
CPP#include <bits/stdc++.h>
using namespace std;
#define maxN 2000005
#define mod 998244353
int n;
int fa[maxN],dep[maxN],r[maxN];//r[x]=0->the same
void init()
{
for(int i = 1; i <= n; i++)
fa[i] = i;
return;
}
int find(int x)
{
if(fa[x] == x)
return x;
int anc = find(fa[x]);
r[x] = (r[fa[x]]+r[x])%2;
fa[x] = anc;
return anc;
}
void merge(int x,int y,int w)
{
int a = find(x),b = find(y);
fa[a] = b;
r[a] = (r[x] + r[y] + w + 1) % 2;
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
init();
int v,opt;
for(int i = 1; i <= n; i++)
{
cin >> v >> opt;
if(find(v) != find(i))
{
merge(i,v,opt);
}
else
{
if((r[v] + r[i] + 1) % 2 != opt)
{
cout << "No answer\n";
return 0;
}
}
}
long long ans = 1,res1 = 0,res2 = 0;
int cnt[maxN][2];
for(int i = 1; i <= n; i++)
cnt[find(i)][r[i]]++;
for(int i = 1; i <= n; i++)
{
if(find(i) == i)
{
ans = (ans * 2) % mod;
res1 += max(cnt[i][0],cnt[i][1]);
res2 += min(cnt[i][0],cnt[i][1]);
}
}
cout << ans << '\n';
cout << res1 << '\n';
cout << res2 << '\n';
return 0;
}
回复
共 4 条回复,欢迎继续交流。
正在加载回复...