[LGR-065] Luo Gu season dated November III Div.2

Approaching $ CSP $ ......

May afternoon played out of competition, I feel so cool.

Very dish I only went for the first two questions ...... but two questions before heard of per capita over ......

Writing is not good card to $ 1067 $ ...... #

T1: basic string exercises :

Prefix and water problems, only need to maintain a minimum prefix to

Dish I chose to use heap maintenance, out of thin air more than a $ log $.

So slow to a **.

Special attention to the situation sentenced to $ 0 $, $ 1 $ of.

Reference Code:

 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 #define N 100005
 5 using namespace std;
 6 char ch[N];
 7 int a[N],ans,n,kkk;
 8 priority_queue<int,vector<int>,greater<int> >q;
 9 int main()
10 {
11     cin>>(ch+1);
12     n=strlen(ch+1);
13     for(int i=1;i<=n;i++)a[i]=a[i-1]+(ch[i]=='1'?-1:1);
14     for(int i=1;i<=n;i++)
15     {
16         //cout<<a[i]<<" ";
17         q.push(a[i]);
18         ans=max(a[i]-q.top(),ans);
19     }
20     if(a[n]==-n)cout<<-1<<endl;
21     else if(a[n]==n)cout<<n<<endl;
22     else cout<<ans<<endl;
23     return 0;
24 }
T1

T2: base shortest exercises

Commentator who told a weird linear approach, but because I was too dishes so thought out.

So I the addition of a $ log $.

First, consider the tree approach, as the only path, so you can find the answer directly doubling the tree

In the absence of any one of the rings and not the $ xor $ 0, so how to take these rings is not important to us, so we can find any of a spanning tree and then practice to get an answer.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define N 300005
 5 using namespace std;
 6 int read()
 7 {
 8     int x=0,f=1;char ch=getchar();
 9     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
10     while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
11     return x*f;
12 }
13 struct node
14 {
15     int a,b,c;
16 }s[N];
17 int n,m,q,f[N],fa[N][22],dist[N][22],v[N],w[N],head[N],nxt[N],cnt,num,dep[N];
18 int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
19 void add(int a,int b,int c)
20 {
21     v[++cnt]=b;
22     w[cnt]=c;
23     nxt[cnt]=head[a];
24     head[a]=cnt;
25 }
26 void dfs(int x,int ff)
27 {
28     dep[x]=dep[ff]+1;
29     for(int i=0;i<=19;i++)
30     {
31         fa[x][i+1]=fa[fa[x][i]][i];
32         dist[x][i+1]=(dist[x][i]^dist[fa[x][i]][i]);
33     }
34     for(int i=head[x];i;i=nxt[i])
35     {
36         if(v[i]==ff)continue;
37         fa[v[i]][0]=x;
38         dist[v[i]][0]=w[i];
39         dfs(v[i],x);
40     }
41 }
42 int lca(int x,int y)
43 {
44     if(dep[x]<dep[y])swap(x,y);
45     for(int i=20;i>=0;i--)
46     {
47         if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
48     }
49     if(x==y)return x;
50     for(int i=20;i>=0;i--)
51     {
52         if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
53     }
54     return fa[x][0];
55 }
56 int qmax(int x,int y)
57 {
58     int ans=0;
59     for(int i=20;i>=0;i--)
60     {
61         if(dep[fa[x][i]]>=dep[y])
62         {
63             ans=(ans^dist[x][i]);
64             x=fa[x][i];
65         }
66     }
67     return ans;
68 }
69 int main()
70 {
71     n=read();m=read();q=read();
72     for(int i=1;i<=m;i++)s[i].a=read(),s[i].b=read(),s[i].c=read();
73     for(int i=1;i<=n;i++)f[i]=i;
74     for(int i=1;i<=m;i++)
75     {
76         int x=s[i].a,y=s[i].b,xx=find(x),yy=find(y);
77         if(xx!=yy)
78         {
79             f[xx]=yy;
80             add(x,y,s[i].c);
81             add(y,x,s[i].c);
82             num++;
83         }
84         if(num==n-1)break;
85     }
86     dfs(1,0);
87     while(q--)
88     {
89         int x=read(),y=read(),xx=lca(x,y);
90         cout<<(qmax(x,xx)^qmax(y,xx))<<endl;
91     }    
92     return 0;
93 }
T2

T3: Gugu Gu

T4: Gugu Gu

Guess you like

Origin www.cnblogs.com/szmssf/p/11852433.html