[HDU3047]Zjnu Stadium

题目

传送门

题解

其实此题和一般思路没什么两样,最关键的就是修改的

w[u]=-w[x]+w[y]+z;

但是这道题还是有一些是需要注意的细节,比如判断两个点的距离关系时只能这样打

if(w[x]-w[y]!=z){
    // puts("It's fake!");
    ++ans;
}

为什么呢?因为我们的修改(就是上面那段代码,还是再粘一遍吧)

w[u]=-w[x]+w[y]+z;

规定了方向性,即我们的 \(x\) 修改为了对于 \(y\) 的距离,那么我们的判断也只能判断 \(w[x]-w[y]\) 而非 \(|w[x]-w[y]|\)

唔,目前似乎也只能这样感性理解了吧?

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

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T qread(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
#undef cg
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=50000;
const int MOD=300;

int fa[MAXN+5],w[MAXN+5];

int n,m;

inline void makeSet(){
    rep(i,1,n)fa[i]=i,w[i]=0;
}

int findSet(const int u){
    if(u^fa[u]){
        int t=fa[u];
        fa[u]=findSet(fa[u]);
        w[u]=w[t]+w[u];
    }return fa[u];
}

int x,y,z,ans,u,v;

signed main(){
    ios::sync_with_stdio(false);
    while(cin>>n>>m){ans=0;
        makeSet();
        while(m--){
            cin>>x>>y>>z;
            u=findSet(x),v=findSet(y);
            if(u==v){
                if(w[x]-w[y]!=z){
                    // puts("It's fake!");
                    ++ans;
                }
            }else{
                fa[u]=v;
                w[u]=-w[x]+w[y]+z;
            }
            // rep(i,1,n)printf("u == %d, fa[u] == %d, w[u] == %d\n",i,fa[i],w[i]);
        }
        cout<<ans<<endl;
    }
	return 0;
}
posted @ 2020-05-19 16:38  Arextre  阅读(110)  评论(0编辑  收藏  举报