cf670 div2ABC problem solution

A Subset Mex is
the largest of mexA and mexB. There must be one missing number in the original array, and the other number is the first missing number in the rest.
Code:

#include <bits/stdc++.h>
#include <algorithm>
#include<iostream>
 
using namespace std;
typedef long long ll;
int a[105];
int vis[105];
 
int main(){
    
    
    int t,n;
    cin>>t;
    while(t--){
    
    
        memset(vis,0,sizeof(vis));
        cin>>n;
        for(int i=1;i<=n;i++)
        {
    
    
            cin>>a[i];
            vis[a[i]]++;
        }
        int ans=0;
        for(int i=0;i<=100;i++){
    
    
            if(vis[i]==0){
    
    
                ans+=i;
                break;
            }
            vis[i]--;
        }
        for(int i=0;i<=100;i++){
    
    
            if(vis[i]==0){
    
    
                ans+=i;
                break;
            }
            vis[i]--;
        }
        cout<<ans<<endl;
    }
}

B Maximum Product is
divided into three situations, four negative numbers, two negative numbers, and no negative numbers, just take the maximum value.
Was unable to find the error before this topic, now I know because if the INF of the macro definition is 0x3f3f3f3f, then it is much smaller than the limit value that longlong can take.
Code:

#include <bits/stdc++.h>
#include <algorithm>
#include<iostream>
#include <stdio.h>
#define INF 9e19
const int maxn=1e5+5;
using namespace std;
typedef long long ll;
ll a[maxn];
int main(){
    
    
    int t,n;
    cin>>t;
    while(t--){
    
    
        memset(a,0,sizeof(a));
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        ll ans=-INF;
        sort(a+1,a+1+n);
        //ans=max(ans,a[1]*a[2]*a[3]*a[4]*a[5]);
        ans=max(ans,a[1]*a[2]*a[3]*a[4]*a[n]);
        //ans=max(ans,a[1]*a[2]*a[3]*a[n-1]*a[n]);
        ans=max(ans,a[1]*a[2]*a[n-2]*a[n-1]*a[n]);
       // ans=max(ans,a[1]*a[n-3]*a[n-2]*a[n-1]*a[n]);
        ans=max(ans,a[n-4]*a[n-3]*a[n-2]*a[n-1]*a[n]);
        cout<<ans<<endl;
    }
}

Question c Link Cut Centroids
dfs The center of gravity of
the tree This topic involves a knowledge point, that is, the center of gravity of the tree. The center of gravity of the tree is involved in the tree dp.

Its properties:
1. All subtrees obtained after deleting the center of gravity, the number of nodes does not exceed 1/2 of the original tree, and a tree has at most two centers of gravity;
2. The sum of the distances from all nodes to the center of gravity in the tree is the smallest, if If there are two centers of gravity, the sum of their distances is equal;
3. The two trees are merged by one edge, and the new center of gravity is on the path of the two centers of gravity of the original tree;
4. The tree deletes or adds a leaf node, and the center of gravity only moves one edge at most ;
5. A tree has at most two centers of gravity, and they are adjacent.

Then we can use property one to determine the center of gravity of the tree.

First build the tree through the adjacency list, use the siz array to store the number of nodes of the tree with u as the root node, and the son array to store the tree with the most nodes remaining after removing this node. The size of son[u] should be connected to u The siz of all nodes of, and the maximum value in n-siz[u]. son[u]=max(son[u],siz[v]), son[u]=max(son[u],n-siz[u]);
Search all nodes connected to the current node by searching dfs Update siz, and son.
At the same time, r1 and r2 store the possible center of gravity. When there is a situation that meets the first nature, assign them a value.
If the tree has only one center of gravity, delete any side of the center of gravity and add it again.
If there are two centers of gravity, delete the connecting edge that connects one center of gravity to any point other than the other center of gravity, and then connect the other center of gravity to this point.

Code:

#include <bits/stdc++.h>
#include <algorithm>
#include<iostream>
#include<vector>
#define INF 9e19
const int maxn=1e5+5;
using namespace std;
typedef long long ll;
vector<int>g[maxn];
int siz[maxn],son[maxn];
int n,r1,r2;
void dfs(int u,int fa){
    
    
    //cout<<"?"<<endl;
    siz[u]=1;
    son[u]=0;
    for(int i=0;i<g[u].size();i++){
    
    
        int v=g[u][i];
        //cout<<"?"<<endl;
        if(v==fa)
            continue;
        dfs(v,u);
        siz[u]+=siz[v];
        son[u]=max(son[u],siz[v]);
    }
    son[u]=max(son[u],n-siz[u]);
	if((son[u]<<1)<=n){
    
    
	    //cout<<"?"<<endl;
        r2=r1;
        r1=u;
    }
}

int main()
{
    
    
    cin.tie(0);
    int t,x,y;
    cin>>t;
    while(t--){
    
    
        cin>>n;
        for(int i=1;i<=n+10;i++)
            g[i].clear();
        memset(siz,0,sizeof(siz));
        memset(son,0,sizeof(son));
        for(int i=1;i<n;i++){
    
    
            cin>>x>>y;
            g[x].push_back(y);
            g[y].push_back(x);
        }
        r1=r2=0;
        dfs(1,0);
        if(!r2){
    
    
              //cout<<"?"<<endl;
              int r3=g[r1][0];
        //cout<<r3<<endl;
            cout<<r1<<" "<<r3<<endl;
            cout<<r1<<" "<<r3<<endl;
        }
        else{
    
    
           // cout<<r2<<" "<<r1<<endl;
            for(int i=0;i<g[r2].size();i++){
    
    
                if(g[r2][i]==r1)
                    continue;
                cout<<r2<<" "<<g[r2][i]<<endl;
                cout<<r1<<" "<<g[r2][i]<<endl;
                break;
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/u011612364/article/details/108564750