zoukankan      html  css  js  c++  java
  • 2816: [ZJOI2012]网络

    传送们

    把一个点拆成c个即可

    浪费时间的水题...

      1 //Achen
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<cstring>
      5 #include<cstdlib>
      6 #include<vector>
      7 #include<cstdio>
      8 #include<queue>
      9 #include<cmath>
     10 #include<set>
     11 #include<map>
     12 #define For(i,a,b) for(int i=(a);i<=(b);i++)
     13 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
     14 const int N=2e5+7;
     15 typedef long long LL; 
     16 typedef double db;
     17 using namespace std;
     18 int n,m,c,q,cnt[N][11];
     19 LL v[N];
     20 
     21 template<typename T> void read(T &x) {
     22     char ch=getchar(); x=0; T f=1;
     23     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
     24     if(ch=='-') f=-1,ch=getchar();
     25     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
     26 }
     27 
     28 map<LL,int>col;
     29 LL get(int x,int y) { return (LL)x*1000000+y; }
     30 
     31 int p[N],ch[N][2],flip[N];
     32 LL mx[N];
     33 #define lc ch[x][0]
     34 #define rc ch[x][1]
     35 void update(int x) { mx[x]=max(max(mx[lc],mx[rc]),v[x]); }
     36 
     37 void down(int x) {
     38     if(!flip[x]) return;
     39     swap(lc,rc);
     40     flip[lc]^=1;
     41     flip[rc]^=1;
     42     flip[x]^=1;
     43 }
     44 
     45 int isroot(int x) { return ch[p[x]][0]!=x&&ch[p[x]][1]!=x; }
     46 
     47 void rotate(int x) {
     48     int y=p[x],z=p[y],l=(x==ch[y][1]),r=l^1;
     49     if(!isroot(y)) ch[z][y==ch[z][1]]=x; p[x]=z;
     50     ch[y][l]=ch[x][r]; p[ch[x][r]]=y;
     51     ch[x][r]=y; p[y]=x;
     52     update(y); update(x);
     53 }
     54 
     55 void splay(int x) {
     56     static int g[N],top=0,tp;
     57     for(tp=x;!isroot(tp);tp=p[tp]) g[++top]=tp;
     58     g[++top]=tp;
     59     while(top) down(g[top--]);
     60     for(;!isroot(x);rotate(x)) {
     61         int y=p[x],z=p[y];
     62         if(!isroot(y)) ((x==ch[y][1])^(y==ch[z][1]))?rotate(x):rotate(y);
     63     }
     64 }
     65 
     66 void access(int x) {
     67     for(int t=0;x;x=p[t=x]) {
     68         splay(x);
     69         rc=t;
     70         update(x);
     71     }
     72 }
     73 
     74 int find_root(int x) {
     75     access(x);
     76     splay(x);
     77     while(lc) x=lc;
     78     return x;
     79 }
     80 
     81 void newroot(int x) {
     82     access(x);
     83     splay(x);
     84     flip[x]^=1;
     85 }
     86 
     87 void lik(int x,int y) {
     88     newroot(x);
     89     splay(x); 
     90     splay(y);
     91     p[x]=y;
     92 }
     93 
     94 void cut(int x,int y) {
     95     newroot(x);
     96     access(y);
     97     splay(y);
     98     if(ch[y][0]==x) ch[y][0]=p[x]=0;
     99     update(y);
    100 }
    101 
    102 void change(int x,int y) {
    103     splay(x);
    104     v[x]=y;
    105     update(x);
    106 }
    107 
    108 void qry(int x,int y) {
    109     if(find_root(x)!=find_root(y)) {
    110         puts("-1"); return;
    111     }
    112     newroot(x);
    113     access(y);
    114     splay(y);
    115     printf("%lld
    ",mx[y]);
    116 }
    117 
    118 #define DEBUG
    119 int main() {
    120 #ifdef DEBUG
    121     freopen("std.in","r",stdin);
    122     //freopen(".out","w",stdout);
    123 #endif
    124     read(n); read(m); read(c); read(q);
    125     For(i,1,n) {
    126         read(v[i]);
    127         For(j,1,c-1) v[i+j*n]=v[i];
    128     }
    129     For(i,1,m) {
    130         int u,v,w;
    131         read(u); read(v); read(w); w++;
    132         cnt[u][w]++; cnt[v][w]++;
    133         col[get(u,v)]=col[get(v,u)]=w;
    134         lik(u+(w-1)*n,v+(w-1)*n); 
    135     }
    136     For(ti,1,q) {
    137         int o,x,y,u,v,nw;
    138         read(o);
    139         if(o==0) {
    140             read(x); LL xx; read(xx);
    141             For(i,1,c) 
    142                 change(x+(i-1)*n,xx);
    143         }
    144         else if(o==1) {
    145             read(u); read(v); read(nw); nw++;
    146             int w=col[get(u,v)];
    147             if(!w) { puts("No such edge."); continue; }
    148             if(w==nw) { puts("Success."); continue; }
    149             if(cnt[u][nw]+1>2||cnt[v][nw]+1>2) { puts("Error 1."); continue; }
    150             if(find_root((nw-1)*n+u)==find_root((nw-1)*n+v)) { puts("Error 2."); continue; }
    151             col[get(u,v)]=col[get(v,u)]=nw;
    152             cnt[u][w]--; cnt[v][w]--;
    153             cnt[u][nw]++; cnt[v][nw]++;
    154             cut((w-1)*n+u,(w-1)*n+v);
    155             lik((nw-1)*n+u,(nw-1)*n+v);
    156             puts("Success.");
    157         }
    158         else {
    159             read(nw); read(u); read(v);
    160             qry(nw*n+u,nw*n+v);
    161         }
    162     }
    163     return 0;
    164 }
    View Code

     

  • 相关阅读:
    php内存管理机制、垃圾回收机制
    Redis 3.2.1集群搭建
    centos开启IPV6配置方法
    /etc/hosts.allow和/etc/hosts.deny详解
    3元购买微信小程序解决方案一个月
    linux下使用ntfs-3g挂载NTFS出错
    腾讯云微信小程序域名变更指南
    nginx开启gzip压缩
    centos 7使用yum安装docker容器
    linux中启动网卡报错:Bringing up interface eth1: Error: Connection activation failed
  • 原文地址:https://www.cnblogs.com/Achenchen/p/8980913.html
Copyright © 2011-2022 走看看