# 2016 Multi-University Training Contest 一

2016 Multi-University Training Contest 1

### 1001-【5723】——Abandoned country

#### 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.

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

6 3.33

#### Author

HIT


#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];

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];

bool vis[MaxN];

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

}

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;

Dp[u] = 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]*Dp[v])+(Dp[u] * (Dp[v]+w*Dp[v])));

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

Dp[u] += Dp[v];

}
}
}

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));

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.

2
1
2 19 20
2
1 19
1 18

#### Sample Output

NO
YES

#include <bits/stdc++.h>

using namespace std;

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

bool mex;

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;

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).

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

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

#### Author

HIT

#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;

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]=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;
}

A+
Date：2016-08-12