带权冰茶姬以及种类冰茶姬回忆录。。。

讲真的,我现在看到语文两个字就恶心

讲道理,真的语文恶心到我了,完全浪费我前一年的时间。。。

食物链(经典)

其实有思维的地方就在合并上,其他就没什么好解释的啊,,,,很水

维护三种关系。。。吃 , 被吃 , 同类

然后每次对3取模就好了

#include<bits/stdc++.h>
#define MAXN 50005
using namespace std;

int n,k,fa[MAXN],sum[MAXN];

int x,y,s,ans;
//tp表示当前节点与父亲节点的关系,0是与父亲节点同类,1是吃父亲节点,2是被父亲节点吃 

int found(int now){
	if(fa[now] == now)return now;
	int zz = found(fa[now]);
	sum[now] = (sum[now] + sum[fa[now]]) % 3;
	fa[now] = zz;
	return zz;
}

int main(){
	cin>>n>>k;
	ans = 0;
	for(int i = 1 ; i <= n ; i++)fa[i] = i , sum[i] = 0;
	for(int i = 1 ; i <= k ; i++){
		cin>>s>>x>>y;s--;
		if(x > n || y > n || (x == y && s == 1)){ans++;continue;}
		int fx = found(x) , fy = found(y);
		if(fx != fy){
			sum[fx] = ((3 - sum[x] + 3) % 3 + s + sum[y]) % 3;
			fa[fx] = fy;
		}
		else if(((3 - sum[y] + 3) % 3 + sum[x]) % 3 != s){
			ans++;continue;
		}
	}
	cout<<ans<<endl;
} 

虫子的生活
同时对2二取模,参考上面的转换方法

#include<bits/stdc++.h>
using namespace std;

int T,n,m,fa[10001],sum[10001],cishu = 0;
int judge = -1;

int found(int x){
    if(fa[x] == x){
        return x;    
    }
    int zz = found(fa[x]);
    sum[x] = (sum[fa[x]] + sum[x]) % 2;
    fa[x] = zz;
    return zz;
}

int main(){
    cin>>T;
    while(T--){
        judge = (-1);
        cishu++;cout<<"Scenario #"<<cishu<<":"<<endl;
        cin>>n>>m;
        for(int i = 1 ; i <= n ; i++)fa[i] = i , sum[i] = 0;
        for(int i = 1 ; i <= m ; i++){
            int x,y;cin>>x>>y;
            int fx = found(x) , fb = found(y);
            if(fx!= fb){
                fa[fx] = fb;
                sum[fx] = (sum[x] + sum[y] + 1) % 2;
            }
            else{
                if((sum[y] + sum[x]) % 2 == 0){
                judge = 1;
                } 
            }
        }
        if(judge == (-1))cout<<"No suspicious bugs found!"<<endl;
        else cout<<"Suspicious bugs found!"<<endl;
        cout<<endl;
    }
} 

还有另一个 hdu3038
这里可以考虑 并查集维护一个前缀和 关联性

就是对于 一个 右端点 接上左端点
然后依次这样。。。

(反正你就 拿个样例过来模拟一下就好了)

就没有了

#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;

long long n,m,xl,yl,s,ans;
long long fa[MAXN],sum[MAXN];

long long found(long long x){
    if(fa[x] == x)return x;
    long long zz = found(fa[x]);
    sum[x] = sum[x] + sum[fa[x]];
    fa[x] = zz;
    return zz;
}

int main(){
	while(scanf("%d%d",&n,&m) == 2){
		ans = 0;
	    for(int i = 0 ; i <= n ; i++)fa[i] = i , sum[i] = 0;
	    for(int i = 1 ; i <= m ; i++){
	        cin>>xl>>yl>>s;xl--;
	        long long fx = found(xl) , fy = found(yl);
	        if(fx != fy){
	            fa[fy] = fx;
	            sum[fy] = sum[xl] - sum[yl] + s;
	        }
	        else{
	            if(sum[yl] - sum[xl] != s)ans++;
	        }
	    }
	    cout<<ans<<endl;	
	}
}
发布了80 篇原创文章 · 获赞 3 · 访问量 1733

猜你喜欢

转载自blog.csdn.net/qq_41567618/article/details/105048791
今日推荐