2018 cattle national day training party day3

D: Very water check-in questions.

#include<bits/stdc++.h>
using namespace std;
int n,m;
double a[1050];
int b[1050];
bool cmp(int x,int y)
{
    return x>y;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        int cnt=0;
        for(int i=0; i<n; i++)
        {
            cin>>a[i]>>b[i];
            if(b[i])
                cnt++;
        }
        sort(a,a+n,cmp);
        double ans=0;
        cnt=min(cnt,m);
        for(int i=0;i<cnt;i++)
        {
            ans+=a[i]/2;
        }
        for(int i=cnt;i<n;i++)
            ans+=a[i];
        printf("%.1f\n",ans);
    }
}

 

H: The meaning of the question is not very clear, but it is found that it has nothing to do with the structure of the tree. As long as we consider how to divide the nodes on the tree into several parts, we can find that it is a modular combination of numbers. We preprocess the factorial and use the multiplication inverse element.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int mod=1e9+7;
ll fac[maxn];
void init_fac()
{
    fac[0]=fac[1]=1;
    for(int i=2;i<maxn;i++)
       fac[i]=i*fac[i-1]%mod;
}
 
ll quickpow(ll a,ll n)
{
    ll ret=1;
    while(n)
    {
        if(n&1) ret=ret*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return ret;
}
ll C(ll a,ll b)
{
    return fac[a]*quickpow(fac[b],mod-2)%mod*quickpow(fac[a-b],mod-2)%mod;
}
 
int main()
{
    int t;
    scanf("%d",&t);
    init_fac();
    while(t--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i=1;i<n;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
        }
        ll ans=C(n-1,m-1)*fac[m]%mod;
        printf("%lld\n",ans);
    }
}

 

A: Ask the horse to walk at least a few steps a day. In the original question, you can consider putting the end point above the bisector of the first quadrant angle, and find out the answer by finding the rule.

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int x, y, ans;
    int t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y;
        x=abs(x);
        y=abs(y);
        if(x>y)swap(x, y);

        if(y==x*2)
        {
            printf("%d\n", (x+y)/3);
            continue;
        }
        if(y<=2*x)
        {
            if(x==1&&y==1)
                ans = 2;
            else if(x==2&&y==2)
                ans = 4;
            else
                ans = (x+y)/3+(x+y)%3;
        }
        else
        {
            ans=x;
            int c=(y-2*x)%4;
            ans+=c;
            ans+=(y-2*x-c)/2;
            if(y==1&&x==0)
                ans=3;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

1: Judge whether it is a bipartite graph

A strange ring must not be a bipartite graph, otherwise it must be a bipartite graph.

At first, I thought about bfs coloring, but found that I couldn't print the ring, so I recorded the two nodes in the ring and used dfs to search the path again, and then burst the memory.

So to print the path, just write dfs directly, but remember to add a mark to judge whether a strange ring is formed. If there is a strange ring, exit the program recursively layer by layer, otherwise dfs will search all the time, and the program will T.

#include<bits/stdc++.h>
using namespace std;
int const maxn=3e5+5;
vector<int>G[maxn];
int color[maxn];
int pre[maxn];
bool iscircle;
bool dfs(int v,int c)
{
    color[v]=c;
    for(int i=0; i<G[v].size(); i++)
    {
        if(color[v]==color[G[v][i]])
        {
            iscircle=true;
            int cnt=0;
            int tem=v;
            int tmp=G[v][i];
            while(v!=tmp)
            {
                cnt++;
                v=pre[v];

            }
            printf("%d\n",cnt+1);
            while(tem!=tmp)
            {
                printf("%d ",tem);
                tem=pre[tem];
            }
            printf("%d ",tem);
            return false;
        }
        if(color[G[v][i]]==0)
        {
            pre[G[v][i]]=v;
            dfs(G[v][i],-c);
            if(iscircle) return false; //Here's the next sign or T
        }
    }
    return true;

}
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1; i<=m; i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    memset(color,0,sizeof(color));
    iscircle=false;
    if(dfs(1,1))
    {
        printf("0\n");
        for(int i=1; i<=n; i++)
        {
            if(color[i]==1)
                printf("1 ");
            else printf("0 ");
        }
    }
}

 

To be continued

Posted by FireyIce01 on Sat, 21 Dec 2019 11:46:24 -0800