Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2743 Accepted Submission(s): 675
Total Submission(s): 2743 Accepted Submission(s): 675
分析:最小生成树+DFS(回溯求边的通过次数,通过次数=边两边点个数的次数);
Problem Description
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 .
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
Source
Recommend
wange2014 | We have carefully selected several similar problems for you:
5746
5745
5744
5743
5742
分析:sg函数;(补);
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iostream>
#define LL long long
using namespace std;
const int N=1e5+10;
const int M=1e6+10;
struct node
{
int u,v;
LL w;
bool operator<(const node &A)const
{
return w<A.w;
}
}que[M];
struct point
{
int v,next;
LL w;
}T[N<<1];
double ans;
int cnt[N];
int head[N];
int top;
int fa[N];
int fint(int x)
{
return fa[x]!=-1?fa[x]=fint(fa[x]):x;
}
void build(int u,int v,LL w)
{
T[top].v=v;
T[top].w=w;
T[top].next=head[u];
head[u]=top++;
}
int t,m;
LL n;
void DFS(int s,int f)
{
cnt[s]=1;
int v;
LL w;
for(int i=head[s];i!=-1;i=T[i].next)
{
v=T[i].v;w=T[i].w;
if(v==f)continue;
DFS(v,s);
cnt[s]+=cnt[v];
ans+=cnt[v]*(n-cnt[v])*w;
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lld%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%lld",&que[i].u,&que[i].v,&que[i].w);
}
top=0;
memset(head,-1,sizeof(head));
sort(que,que+m);
memset(fa,-1,sizeof(fa));
LL sum=0;
int cnt=0;
int u,v;
for(int i=0;i<m;i++)
{
u=fint(que[i].u),v=fint(que[i].v);
if(u!=v)
{
fa[u]=v;
build(que[i].u,que[i].v,que[i].w);
build(que[i].v,que[i].u,que[i].w);
cnt++;
sum+=que[i].w;
if(cnt==n-1)
break;
}
}
ans=0.0;
DFS(1,-1);
//printf("--------%lf\n",ans);
printf("%I64d %.2lf\n",sum,ans*2.0/(n*(n-1)));
}
return 0;
}
Chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1075 Accepted Submission(s): 462
Total Submission(s): 1075 Accepted Submission(s): 462
分析:sg函数;(补);
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.
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
Author
HIT
Source
Recommend
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int N=(1<<21);
int sg[N+10];
bool vis[20];
int get_sg(int x)
{
memset(vis,0,sizeof(vis));
for(int i=19;i>=0;i--)
{
if(x&(1<<i))
{
int tmp=x;
for(int j=i-1;j>=0;j--)
{
if(!(tmp&(1<<j)))
{
tmp^=(1<<i)^(1<<j);
vis[sg[tmp]]=1;
break;
}
}
}
}
for(int i=0;i<20;i++)
if(!vis[i])
return i;
}
int main()
{
memset(sg,0,sizeof(sg));
for(int i=0;i<N;i++)
{
sg[i]=get_sg(i);
}
int t,n,m,x;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int ans=0;
int tmp;
for(int i=0;i<n;i++)
{
scanf("%d",&m);
tmp=0;
while(m--)
{
scanf("%d",&x);
tmp|=(1<<(20-x));
}
ans^=sg[tmp];
}
if(ans)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1809 Accepted Submission(s): 587
Total Submission(s): 1809 Accepted Submission(s): 587
分析:RMQ+二分;
Problem Description
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.
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) .
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
Source
Recommend
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <iostream>
#include <map>
#define LL long long
using namespace std;
const int N=1e6+10;
int dp[N][20];
map<int,LL>vis;
void RMQ_init(int n)
{
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;(i+(1<<j)-1)<=n;i++)
{
dp[i][j]=__gcd(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
}
void init()
{
vis.clear();
}
int RMQ(int l,int r)
{
int k=0;
while((1<<(k+1))<=r-l+1)k++;
return __gcd(dp[l][k],dp[r-(1<<k)+1][k]);
}
int L[N],R[N];
int main()
{
int t,n,m;
int cnt=0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&dp[i][0]);
}
RMQ_init(n);
init();
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&L[i],&R[i]);
int num=RMQ(L[i],R[i]);
vis[num]=0;
}
int l,r,mid,d1,d2,ans,nex;
for(int i=1;i<=n;i++)
{
nex=i;
while(nex<=n)
{
d1=RMQ(i,nex);
l=nex,r=n;
while(l<=r)
{
mid=(l+r)>>1;
d2=RMQ(i,mid);
if(d2>=d1)l=mid+1,ans=mid;
else
r=mid-1;
}
if(vis.find(d1)!=vis.end())
vis[d1]+=ans-nex+1;
nex=r+1;
}
}
printf("Case #%d:\n",++cnt);
for(int i=1;i<=m;i++)
{
ans=RMQ(L[i],R[i]);
printf("%d %lld\n",ans,vis[ans]);
}
}
return 0;
}