acwing week 6


An undirected connected graph consisting of n points and m edges is given.

We want to turn it into a complete graph through a series of operations (that is, each pair of different vertices has exactly one edge connected).

During each operation, you can select one of the points, find all the points directly connected to it, and connect the edges between these points (if there are already edges between the two points, there is no need to connect them repeatedly).

How many operations can you change the whole graph into a complete graph?

Input format
The first line contains two integers n,m.

The next m lines contain two integers u and V, indicating that there is an edge between point u and point v.

All points are numbered 1 ∼ n.

Output format
The first line outputs the minimum number of operations.

The second line outputs the number of points selected for each operation, separated by spaces between integers. If the minimum number of operations is 0, the second line does not need to be output.

If the answer is not unique, any reasonable scheme can be output.

Data range
The first three test points meet 1 ≤ N and m ≤ 10.
All test points meet 1 ≤ n ≤ 22, 0 ≤ m ≤ n(n − 1) 2, 1 ≤ u,v ≤ n, u ≠ v.
Ensure that there are no double edges and self rings, and that the given graph is connected.
Idea:
Gradual outward expansion, analogy prim algorithm
One great regiment keeps annexing other regiments
That is, after one core links another core, the original point set will be expanded
Until the status is 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
Practice:
1. Initialize init
f [status] = operand
e [point j] = connected state of this point
g [state 2] = {state 1, j} indicates that state 2 is the state after the point set owned by j is connected by state 1
2. Enumerate all States from small to large i
Then enumerate the point set linked by i, that is, enumerate j points, and then go to the state of k
k=i or e [j]
3. Output the last full state 111111, that is, f [2^n-1]

#include<bits/stdc++.h>
#define x first
#define y second

using namespace std;
const int N = 22, M=1<<22;
typedef pair<int,int> PII;
int n,m;
int f[M];
int e[N];
PII g[M];
int main(){
    cin>>n>>m;
    if(2*m==n*(n-1)){
        puts("0");
        return 0;
    }
    memset(f,0x3f,sizeof f);
    for(int i=0;i<n;i++)e[i]=1<<i;
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        x--,y--;
        e[x]|=1<<y;
        e[y]|=1<<x;
    }
    for(int i=0;i<n;i++)f[e[i]]=1,g[e[i]]={0,i};
    //During each operation, you can select one of the points, find all the points directly connected to it, and connect the edges between these points
    for(int i=0;i<1<<n;i++){
        //Enumerate the point sets for each state connection
        for(int j=0;j<n;j++){
            if(f[i]==0x3f3f3f3f)continue;
            if(i>>j&1){//Cluster i is connected to point j
                int k=i|e[j];//The new larger regiment is obtained from the original regiment i and the upper small regiment j
                if(f[k]>f[i]+1){
                    f[k]=f[i]+1;//State k is obtained by connecting state i to point j
                    g[k]={i,j};//State k is obtained by connecting state i to point j
                }
            }
        }
    }
    int k=(1<<n)-1;
    cout<<f[k]<<endl;
    
    while(k){
        cout<<g[k].y+1<<" ";
        k=g[k].x;
    }
    //Recursive output, state k is state x before it is connected to j, so which state x is connected to which point?
    //Here we deal with recursion
    
}


Use f(x) to represent the minimum positive integer a satisfying the following conditions:

a≥x.
Each digit of a does not contain other digits except 4 and 7.
Now, given two integers l,r(l ≤ r), please calculate f(l)+f(l+1) +... + F ® Value of.

Input format
One line, two integers l,r.

Output format
A line, an integer, represents the sum of the results.

Data range
The first three test points meet 1 ≤ l ≤ r ≤ 10,
All test points meet 1 ≤ l ≤ r ≤ 109.

The one above is state compression dp

The following is a naked thinking problem. The game was not written at that time, but it was written soon after going out to play a ball
There are two versions

#include<bits/stdc++.h>
using namespace std;
#define int long long
int l,r,init[1000010],ans,ansl,ansr,sum[1000010];
int a,p;
signed main(){
    cin>>l>>r;
    init[1]=4;
    init[2]=7;
    sum[1]=4;
    sum[2]=11;
    a=4;
    p=2;
    for(int i=1;i<2048;i++){
        init[++p]=a*10+4;
        init[++p]=a*10+7;
        a=init[p-i-1];
    }
    int i=1;
    while(l>init[i])i++;
    int ansl=i;
    while(r>init[i])i++;
    int ansr=i;
    if(ansr>ansl){
        ans+=(1+init[ansl]-l)*init[ansl];
        ans+=(r-init[ansr-1])*init[ansr];
    }
    else {
        ans=init[ansl]*(r-l+1);
    }
    if(ansr-ansl>1){
        for(int p=ansl+1;p<ansr;p++){
            ans+=init[p]*(init[p]-init[p-1]);
        }
    }
    cout<<ans<<endl;
}

Second version

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long LL;

vector<LL> S;

void dfs(int u, LL x)
{
    S.push_back(x);
    if (u == 10) return;
    dfs(u + 1, x * 10 + 4);
    dfs(u + 1, x * 10 + 7);
}

int main()
{
    dfs(0, 0);
    sort(S.begin(), S.end());

    LL l, r;
    cin >> l >> r;
    LL res = 0;
    for (int i = 1; i < S.size(); i ++ )
    {
        LL a = S[i - 1] + 1, b = S[i];
        res += S[i] * max(0ll, (min(r, b) - max(l, a) + 1));
    }
    cout << res << endl;

    return 0;
}

Author: yxc
 Link: https://www.acwing.com/activity/content/code/content/1408596/
Source: AcWing
 The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

yxc uses the initialization of dfs burst search
Thief niubi
Yes, I learned another little skill

Keywords: Algorithm Graph Theory

Added by marian on Sat, 22 Jan 2022 04:46:18 +0200