2016 Multi-University Training Contest 一

2016 Multi-University Training Contest 1

1001-【5723】——Abandoned country

Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description

An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000). Guaranteed that any two wi are different. The roads made all the villages connected directly or indirectly before destroyed. Every road will cost the same value of its length to rebuild. The king wants to use the minimum cost to make all the villages connected with each other directly or indirectly. After the roads are re-built, the king asks a men as messenger. The king will select any two different points as starting point or the destination with the same probability. Now the king asks you to tell him the minimum cost and the minimum expectations length the messenger will walk.

Input

The first line contains an integer T(T≤10) which indicates the number of test cases.

For each test case, the first line contains two integers n,m indicate the number of villages and the number of roads to be re-built. Next m lines, each line have three number i,j,wi, the length of a road connecting the village i and the village j is wi.

Output

output the minimum cost and minimum Expectations with two decimal places. They separated by a space.

Sample Input

1
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6

Sample Output

6 3.33

Author

HIT

题解:求解图的最小生成树以及最小生成树的任意两点之间距离的期望,对于给定的一个联通的图,我们可以将最小生成树建立出来,那么如果要求距离的期望,如果我们知道树中的任意所有点之间的距离和s,那么期望就是sn(n1)2,我们定义Dp[i][0],表示以i为根节点的树到i的距离和Dp[i][1]表示到达i的方案,那么对于一个孩子节点v,ans为最后的树的距离和,Dp[v][0]+Dp[v][1]w为树v到i的距离和,Dp[u][0]*Dp[v][1]+Dp[i][1](Dp[v][0]+Dp[v][1])表示两个树之间节点相互到达的距离和(想想为什么?),那么ans+=((Dp[u][0]Dp[v][1])+(Dp[u][1] (Dp[v][0]+w*Dp[v][1])))进行更新答案,然后更新Dp即可。


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int MaxN = 110000;

const int MaxM = 1100000;

typedef struct node
{
    int v,w,next;

}Node;

Node E[MaxM];

int Head[MaxN],top;

typedef struct OO
{
    int u,v,w;

    bool operator < (const OO &a)const
    {
        return w<a.w;
    }

}Map;

Map mp[MaxM];

int pre[MaxN];

LL n,m;

double  sum;

LL Dp[MaxN][2];

bool vis[MaxN];

void Creat(int u,int v,int w)
{
    E[top].v = v;E[top].w = w;

    E[top].next = Head[u];

    Head[u] = top++;
}

int Find(int x)
{
    return pre[x] == 0?x:pre[x] = Find(pre[x]);
}

void Union(int x,int y)
{
    int Fx  = Find(x);

    int Fy = Find(y);

    if(Fx != Fy)
    {
        pre[Fx] = Fy;
    }
}

void dfs(int u)\树Dp
{
    vis[u] = true;

    Dp[u][0] = 0;

    Dp[u][1] = 1;

    for(int i = Head[u]; i!=-1;i = E[i].next)
    {
        int v = E[i].v;

        int w = E[i].w;

        if(!vis[v])
        {
            dfs(v);

            sum  += ((Dp[u][0]*Dp[v][1])+(Dp[u][1] * (Dp[v][0]+w*Dp[v][1])));

            Dp[u][0] +=(Dp[v][0]+Dp[v][1]*w);

            Dp[u][1] += Dp[v][1];

        }
    }
}

int main()
{
    int T;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%lld %lld",&n,&m);

        for(int i = 0;i<m;i++)
        {
            scanf("%d %d %d",&mp[i].u,&mp[i].v,&mp[i].w);
        }

        sort(mp,mp+m);

        memset(pre,0,sizeof(pre));

        memset(Head,-1,sizeof(Head));top = 0;

        LL ans = 0;

        int num = 0;

        for(int i = 0;i<m;i++)\最小生成树
        {
            if(Find(mp[i].u) != Find(mp[i].v))
            {
                ans += mp[i].w;

                num++;

                Union(mp[i].u,mp[i].v);

                Creat(mp[i].u,mp[i].v,mp[i].w);

                Creat(mp[i].v,mp[i].u,mp[i].w);
            }

            if(num == n-1) break;
        }

        sum = 0;

        memset(vis,false,sizeof(vis));

        dfs(1);

        printf("%lld %.2fn",ans,sum/(n*(n-1)/2));
    }
    return 0;
}

1002-【5724】Chess

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Alice and Bob are playing a special chess game on an n × 20 chessboard. There are several chesses on the chessboard. They can move one chess in one turn. If there are no other chesses on the right adjacent block of the moved chess, move the chess to its right adjacent block. Otherwise, skip over these chesses and move to the right adjacent block of them. Two chesses can’t be placed at one block and no chess can be placed out of the chessboard. When someone can’t move any chess during his/her turn, he/she will lose the game. Alice always take the first turn. Both Alice and Bob will play the game with the best strategy. Alice wants to know if she can win the game.

Input

Multiple test cases.

The first line contains an integer T(T≤100), indicates the number of test cases.

For each test case, the first line contains a single integer n(n≤1000), the number of lines of chessboard.

Then n lines, the first integer of ith line is m(m≤20), indicates the number of chesses on the ith line of the chessboard. Then m integers pj(1≤pj≤20) followed, the position of each chess.

Output

For each test case, output one line of “YES” if Alice can win the game, “NO” otherwise.

Sample Input

2
1
2 19 20
2
1 19
1 18

Sample Output

NO
YES

题解:博弈的问题 ,由于每一行只有20个格子,那么我们就可以将他们的状态状压一下,跑一个SG函数,然后像Nim博弈瞎搞一下就可以了

#include <bits/stdc++.h>

using namespace std;

int Sg[(1<<20)+10];

bool mex[21];

int GetSg(int s)
{
    memset(mex,false,sizeof(mex));

    for(int i = 0;i<20;i++)
    {
        if((1<<i)&s)
        {
            for(int j = i-1;j>=0;j--)
            {
                if(((1<<j)&s) == 0)
                {
                    mex[Sg[((1<<i)^s)^(1<<j)]] = true;

                    break;
                }
            }
        }
    }

    for(int i  = 0;i<=20;i++)
    {
        if(!mex[i]) return i;
    }
}

int main()
{
    Sg[0] = 0;

    for(int i = 1;i<(1<<20);i++)
    {
        Sg[i] = GetSg(i);

    }

    int T,n,m,st;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d",&n);

        int ans = 0;

        for(int i = 0;i<n;i++)
        {
            scanf("%d",&m);

            int num = 0;

            for(int j = 0;j<m;j++)
            {
                scanf("%d",&st);

                num = (num^(1<<(20-st)));

            }

            ans ^=Sg[num];
        }

        if(ans)
        {
            printf("YESn");
        }
        else printf("NOn");
    }

    return 0;
}

1004【5726】-GCD

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Give you a sequence of N(N≤100,000) integers : a1,…,an(0 < ai≤1000,000,000). There are Q(Q≤100,000) queries. For each query l,r you have to calculate gcd(al,,al+1,…,ar) and count the number of pairs(l′,r′)(1≤ l < r≤N)such that gcd(al′,al′+1,…,ar′) equal gcd(al,al+1,…,ar).

Input

The first line of input contains a number T, which stands for the number of test cases you need to solve.

The first line of each case contains a number N, denoting the number of integers.

The second line contains N integers, a1,…,an(0 < ai≤1000,000,000).

The third line contains a number Q, denoting the number of queries.

For the next Q lines, i-th line contains two number , stand for the li,ri, stand for the i-th queries.

Output

For each case, you need to output “Case #:t” at the beginning.(with quotes, t means the number of the test case, begin from 1).

For each query, you need to output the two numbers in a line. The first number stands for gcd(al,al+1,…,ar) and the second number stands for the number of pairs(l′,r′) such that gcd(al′,al′+1,…,ar′) equal gcd(al,al+1,…,ar).

Sample Input

1
5
1 2 4 6 7
4
1 5
2 4
3 4
4 4

Sample Output

Case #1:
1 8
2 4
2 4
6 1

Author

HIT

题解:题意就不说了,如何快速的求解区间的GCD,可以用线段树或者rmq,那么如果我们能够处理出区间的GCD,如何求解出整个区间的GCD等于所询问区间的个数,假设我们固定区间的左端点,那么我们会发现随着右端点的移动,GCD是非增的,我们可以通过二分来确定某个GCD的右端点,那么我们就可以在nlogn的时间中处理出所有区间的GCD,用map记录一下即可,不过线段树会被卡常,用rmq可过。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;

const int Max = 110000;

int arr[Max];

int f[100010][18];

int n;

map<int,long long >M;

int GCD(int a,int b)
{
    return b == 0?a:GCD(b,a%b);
}

void rmq()
{
    for(int j=1; j<=n; j++) f[j][0]=arr[j];

    for(int i=1; i<18; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(j+(1<<i)-1 <= n)
            {
                f[j][i]=GCD(f[j][i-1],f[j+(1<<(i-1))][i-1]);
            }
        }
    }
}
int look(int l,int r)
{
    int k=(int)log2((double)(r-l+1));
    return GCD(f[l][k],f[r-(1<<k)+1][k]);
}

void Op(int s)
{
    int r =s;

    int g;

    while(r<=n)
    {
        g = look(s,r);

        int L = r,R = n;

        while(L<R)
        {

            int mid = (L+R+1)>>1;

            if(look(s,mid) == g)
            {
                L = mid;
            }
            else R = mid-1;
        }
        M[g]+=(L-r+1);

        r = L+1;

    }
}

int main()
{


    int T;

    int z = 1;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d",&n);

        for(int i = 1; i<=n; i++) scanf("%d",&arr[i]);

        rmq();

        M.clear();

        for(int i = 1; i<=n; i++)
        {
            Op(i);
        }

        int m;

        scanf("%d",&m);

        int l,r;
        printf("Case #%d:n",z++);

        while(m--)
        {
            scanf("%d %d",&l,&r);

            int g = look(l,r);

            printf("%d %lldn",g,M[g]);
        }
    }
    return 0;
}

相关内容推荐