[and collect] Luogu P4865 Samcompu Loves Water

Keywords: MySQL

Topic Description

Samcompu needs to develop a water plan. The main purpose of this plan is to avoid the time the teacher monitors.

Teachers will leave the computer room TT times in the middle and tim_itimi seconds in the second. Samcompu's paddling is not random. He may have "water" resources. In his inventory, there are NN websites that can be watered. Samcompu has a black technology that allows him to jump between websites and save the information of the jumped pages in seconds at virtually no time. That is to say, Samcompu does not need to spend time browsing the web page every time it jumps. Of course, this is limited to the N-1N_1 jump between NN sites (to ensure that each site can jump to all other sites). For the ii jump mode, there is a dangerous degree of w_iwi in the jump from the u_iui website to the v_ivi website. This dangerous value may cause computer jam. If Samcompu can not be handled in time, it will be found perfectly by the teacher.

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

The longer the teacher walks, the higher the upper limit on the risk of computer jam before being discovered by the teacher. Simply put, they are in direct proportion, with a proportional coefficient of 1.

Unfortunately, Samcompu's black technology is not stable. When the teacher leaves for the first time, the K_iKi jump is not available.

Of course, every water can start from any website or end from any website.

Now Samcompu wants to know how many different safe water schemes he can have when the ii teacher leaves the computer room. The two water schemes are different if and only if the first or last site of the two water schemes is different.

(Supplementary Note: A safe water scheme if and only if there is no jumping mode ii in the jumping path when the teacher leaves the classroom for the j j time makes tim_j leqslant w_itimj wi, the unavailable jumping mode will be restored after each water is over. )

 

 

Problem Solution

  • We rank all the jump modes, and then calculate them when merging according to the way of adding edges to the merge set.
  • So what about the failed jump mode?
  • So we can specially take out these possible failures of jump mode, when dealing with a query, first back up the previous array, then violence plus the need to add edges, record the answers and then directly restore the original array.

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 }

Posted by hip_hop_x on Mon, 22 Jul 2019 22:46:01 -0700