[Disjoint-set] Luogu P4865 Samcompu Loves Water

Title Description

Samcompu need to develop a water plan. The main purpose of this project is to monitor the runoff to avoid time teacher.

The teacher will leave room in the middle T T th, I I times will leave tim_i T I m I sec. Samcompu when the water is not water lightly. But he has "water" resources. He had in his inventory N N a can of water sites. Samcompu have a kind of black science and technology, he can hardly consume any time to jump from site to site with the site and the information kept jumping pages of seconds. In other words, Samcompu not need to spend time every time jump to browse the web. Of course, only limited N between N sites N. 1- N - . 1 Goto embodiment (to ensure that each site can jump to another all the sites). For the first i i kind of jump method, the first u_i U i sites to the first v_i v i jump websites there is a degree of risk w_i w i , this value may cause dangerous computer stuck, if not timely Samcompu processing, it will be found perfect teacher.

It is worth mentioning that, after the water meter checked many times, Samcompu summed up a rule:

The longer the teacher go, to ensure a good deal higher limit computer stuck degree of danger before being found teacher. In simple terms, the relationship between the two is proportional to the scale factor of 1.

Unfortunately, Samcompu black science and technology is not stable, the first teacher i leave when i times, the first K_i K i Goto mode is not available.

Of course, every time the water can begin from any website, or any website from the end.

Now Samcompu want to know, for the first i i times when the teacher left the room, he how many different safe water program to have. Two Water different programs if and only if a site different from the first program of the last two of water or a website.

(Supplementary note: a safe water solution if and only if this is a teacher of j absence of a jump jump way path leaving the j-th class I I such that tim_j \ leqslant W_i T I m j W I each time the water after the jump method is not available will be restored.)

 

 

answer

  • We put all the way to jump to row a sequence, and then follow disjoint-set bordered way, the calculation time of the merger
  • So how the failure to get a jump way?
  • So you can put these may fail to jump out a special way, to back up a good array before processing a query, then you need to add violence plus side, after recording the answers directly restore the original array like

Code

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 const int N=100010;
 7 int t,n,cnt,ans[N],fa[N],now[N],sl[N],last;
 8 struct node{ int p,q,num; }Q[N];
 9 struct edge{ int from,to,v,num; }e[N];
10 bool cmp(node x,node y){ return x.q<y.q||x.q==y.q&&x.p<y.p; }
11 bool cmp1(edge x,edge y){ return x.v<y.v; }
12 void insert(int u,int v,int w,int id){ e[++cnt].from=u,e[cnt].to=v,e[cnt].v=w,e[cnt].num=id; }
13 int find(int x){ return fa[x]==x?x:fa[x]=find(fa[x]); }
14 int count(int x){ return x*(x-1); }
15 int main()
16 {
17     scanf("%d%d",&t,&n);
18     for (int i=1,x,y,z;i<n;i++) scanf("%d%d%d",&x,&y,&z),insert(x,y,z,i); 
19     for (int i=1;i<=t;i++) scanf("%d%d",&Q[i].p,&Q[i].q),Q[i].num=i;
20     sort(Q+1,Q+1+t,cmp),sort(e+1,e+n,cmp1);
21     for (int i=1;i<n;i++) now[e[i].num]=i;
22     for (int i=1;i<=t;i++)
23     {
24         if (i==1||Q[i].q!=Q[i-1].q)
25         {
26             for (int j=1;j<=n;j++) fa[j]=j,sl[j]=1;
27             last=1,ans[Q[i].num]=0;
28         }
29         else ans[Q[i].num]=ans[Q[i-1].num];
30         while (last<n&&e[last].v<Q[i].p)
31         {
32             if (last!=now[Q[i].q]&&find(e[last].from)!=find(e[last].to))
33             {
34                 int now1=find(e[last].from),now2=find(e[last].to);
35                 ans[Q[i].num]+=count(sl[now1]+sl[now2])-count(sl[now1])-count(sl[now2]),sl[now2]+=sl[now1];sl[now1]=0,fa[now1]=now2;
36             }
37             last++;
38         }
39     }
40     for (int i=1;i<=t;i++) printf("%d\n",ans[i]);
41 }

 

Guess you like

Origin www.cnblogs.com/Comfortable/p/11231285.html