社区讨论
90分求调,就第8个超时
P2024[NOI2001] 食物链参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @mjiei6oh
- 此快照首次捕获于
- 2025/12/23 17:47 2 个月前
- 此快照最后确认于
- 2025/12/25 23:55 2 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
int a[10000];
int ship[10000];
int ans=0;
void chushi(int n){
for(int i=0;i<=n;i++){
a[i]=i;ship[i]=0;
}
}
int find(int x){
if(x!=a[x]){
int t=a[x];
a[x]=find(a[x]);
ship[x]=(ship[x]+ship[t])%3;
}
return a[x];
}
void chacuo(int x,int y,int rship){
int rootx=find(x);
int rooty=find(y);
if(rootx==rooty){
if(rship-1!=(ship[x]-ship[y]+3)%3) ans++;
}
else{
a[rootx]=rooty;
ship[rootx]=(ship[y]-ship[x]+rship-1)%3;
}
}
int main(){
ios::sync_with_stdio(0);
int n,k;
cin>>n>>k;
chushi(n);
for(int i=0;i<k;i++){
int rship,x,y;
cin>>rship>>x>>y;
if(x < 1 || x > n || y < 1 || y > n) ans++;
else chacuo(x,y,rship);
}
cout<<ans<<endl;
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...