NOIP 模拟 $27\; \rm 牛半仙的妹子图$

题解 \(by\;zj\varphi\)

颜色数很少,考虑枚举颜色数。

建出来一棵最小生成树,可以证明在最小生成树上,一个点到另一个点的路径上的最大权值最小(易证,考虑 \(\rm kruskal\) 的原理)。

在最小生成树上 \(dfs\) 一遍,求出到达每种颜色的最小权值,询问时枚举每种颜色即可。

Code:
#include<bits/stdc++.h>
#define ri register signed
#define p(i) ++i
using namespace std;
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf,OPUT[100];
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++;
    template<typename T>inline void read(T &x) {
        ri f=1;x=0;register char ch=gc();
        while(!isdigit(ch)) {if (ch=='-') f=0;ch=gc();}
        while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
        x=f?x:-x;
    }
    template<typename T>inline void print(T x,char t) {
        if (x<0) putchar('-'),x=-x;
        if (!x) return putchar('0'),(void)putchar(t);
        ri cnt(0);
        while(x) OPUT[p(cnt)]=x%10,x/=10;
        for (ri i(cnt);i;--i) putchar(OPUT[i]^48);
        return (void)putchar(t);
    }
}
using IO::read;using IO::print;
namespace nanfeng{
    #define FI FILE *IN
    #define FO FILE *OUT
    template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
    template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
    typedef long long ll;
    static const int N=5e5+7;
    int c[N],first[N],fa[N],dis[N],vis[N],st[N],cnt,t=1,n,m,q,opt,x,MOD;
    ll ans;
    struct edge{int v,nxt,w;}I[N],e[N<<1];
    inline void add(int u,int v,int w) {
        e[t].v=v,e[t].w=w,e[t].nxt=first[u],first[u]=t++;
        e[t].v=u,e[t].w=w,e[t].nxt=first[v],first[v]=t++;
    }
    inline int operator<(const edge &e1,const edge &e2) {return e1.w<e2.w;}
    int find(int x) {return fa[x]==x?x:fa[x]=find(fa[x]);}
    inline void kruskal() {
        sort(I+1,I+m+1);
        for (ri i(1);i<=n;p(i)) fa[i]=i;
        for (ri i(1);i<=m;p(i)) {
            int u=I[i].v,v=I[i].nxt,w=I[i].w;
            if (find(u)==find(v)) continue;
            fa[find(u)]=v;
            add(u,v,w);
        }
    }
    void dfs(int x,int fa,int mx) {
        dis[c[x]]=cmin(dis[c[x]],mx);
        for (ri i(first[x]),v;i;i=e[i].nxt) {
            if ((v=e[i].v)==fa) continue;
            dfs(v,x,cmax(mx,e[i].w));
        }
    }
    inline int main() {
        //FI=freopen("nanfeng.in","r",stdin);
        //FO=freopen("nanfeng.out","w",stdout);
        read(n),read(m),read(q),read(x),read(opt);
        if (opt) read(MOD);
        for (ri i(1);i<=n;p(i)) {
            read(c[i]);
            if (!vis[c[i]]) vis[c[i]]=1,st[p(cnt)]=c[i]; 
        }
        for (ri i(1);i<=m;p(i)) read(I[i].v),read(I[i].nxt),read(I[i].w);
        kruskal();
        memset(dis,127,sizeof(dis));
        dfs(x,0,0);
        for (ri i(1);i<=q;p(i)) {
            register ll l,r;
            read(l),read(r);
            if (opt) {
                (l^=ans)%=MOD,(r^=ans)%=MOD;
                l+=1,r+=1;
                if (l>r) swap(l,r);
            }
            ans=0;
            for (ri j(1);j<=cnt;p(j)) {
                if (dis[st[j]]<=l) ans+=r-l+1; 
                else if (dis[st[j]]<=r) ans+=r-dis[st[j]]+1;
            }  
            print(ans,'\n');
        }
        return 0;
    }
}
int main() {return nanfeng::main();}
上一篇:机房测试:lunch(贪心+最短路)


下一篇:ch_POJ2182 Lost Cows