您的位置:首页 > 其它

2013 ACM-ICPC吉林通化全国邀请赛(hdu 4493 - 4599)(并查集+dp+数学+判奇环+差分约束)

2015-07-11 16:49 363 查看

D-City

Problem Description

Luxer is a really bad guy. He destroys everything he met.

One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input.

Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.

Input

First line of the input contains two integers N and M.

Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line.

Constraints:

0 < N <= 10000

0 < M <= 100000

0 <= u, v < N.

Output

Output M lines, the ith line is the answer after deleting the first i edges in the input.

Sample Input

5 10

0 1

1 2

1 3

1 4

0 2

2 3

0 4

0 3

3 4

2 4

Sample Output

1

1

1

2

2

2

2

3

4

5

Hint

The graph given in sample input is a complete graph, that each pair of vertex has an edge connecting them, so there’s only 1 connected block at first.

The first 3 lines of output are 1s because after deleting the first 3 edges of the graph, all vertexes still connected together.

But after deleting the first 4 edges of the graph, vertex 1 will be disconnected with other vertex, and it became an independent connected block.

Continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output should always be N.

题意:按照给出的边的顺序每次删除前k条,能省下多少个联通快

思路:倒序处理,并查集判断是不是减少联通快的数量

[code]#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
struct Q
{
    int x,y;
}q[maxn];
int N,M;
int pre[maxn];
int ans[maxn];
int find(int x)
{
    if(x==pre[x])return x;
    return pre[x]=find(pre[x]);
}
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        for(int i=1;i<=M;i++)
            scanf("%d%d",&q[i].x,&q[i].y);
        for(int i=0;i<=N;i++)pre[i]=i;
        ans[M]=N;
        for(int i=M;i>0;i--)
        {
            int x=find(q[i].x),y=find(q[i].y);
            if(x!=y)ans[i-1]=ans[i]-1;
            else ans[i-1]=ans[i];
            pre[x]=y;
        }
        for(int i=1;i<=M;i++)printf("%d\n",ans[i]);
    }
    return 0;
}


GCD and LCM

Problem Description

Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L?

Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.

Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.

Input

First line comes an integer T (T <= 12), telling the number of test cases.

The next T lines, each contains two positive 32-bit signed integers, G and L.

It’s guaranteed that each answer will fit in a 32-bit signed integer.

Output

For each test case, print one line with the number of solutions satisfying the conditions above.

Sample Input

2

6 72

7 33

Sample Output

72

0

题意:告诉三个数的GCD,LCM,求这样的排列有多少种

思路:首先分解质因数,假设对于质因数x,三个数分别有ai,bi,cia_i,b_i,c_i个,LCM中有ziz_i个,那么这三个数中至少有一个为ziz_i,还至少有一个0,剩下的随便选,但要注意剩下的那个选0和ziz_i的时候

[code]#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=500;

LL G,L;
int cnt;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d%I64d",&G,&L);
        if(L%G){printf("0\n");continue;}
        L/=G;
        LL ans=1;
        LL k=sqrt(L);
        for(LL i=2;i<=k;i++)
        {
            if(L%i)continue;
            LL num=0;
            while(L%i==0)
            {
                L/=i;
                num++;
            }
            ans=ans*6*num;
        }
        if(L!=1)ans*=6;
        printf("%I64d\n",ans);
    }
    return 0;
}


Cannon

Problem Description

In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or vertically along the chess grid. At each move, it can either simply move to another empty cell in the same line without any other chessman along the route or perform an eat action. The eat action, however, is the main concern in this problem.

An eat action, for example, Cannon A eating chessman B, requires two conditions:

1、A and B is in either the same row or the same column in the chess grid.

2、There is exactly one chessman between A and B.

Here comes the problem.

Given an N x M chess grid, with some existing chessmen on it, you need put maximum cannon pieces into the grid, satisfying that any two cannons are not able to eat each other. It is worth nothing that we only account the cannon pieces you put in the grid, and no two pieces shares the same cell.

Input

There are multiple test cases.

In each test case, there are three positive integers N, M and Q (1<= N, M<=5, 0<=Q <= N x M) in the first line, indicating the row number, column number of the grid, and the number of the existing chessmen.

In the second line, there are Q pairs of integers. Each pair of integers X, Y indicates the row index and the column index of the piece. Row indexes are numbered from 0 to N-1, and column indexes are numbered from 0 to M-1. It guarantees no pieces share the same cell.

Output

There is only one line for each test case, containing the maximum number of cannons.

Sample Input

4 4 2

1 1 1 2

5 5 8

0 0 1 0 1 1 2 0 2 3 3 1 3 2 4 0

Sample Output

8

9

思路:暴搜,我是枚举的每一行的状态,也可以枚举每一个格子的状态

[code]const int maxn=10;
const int maxm=(1<<5);
int N,M,Q;
int ans,S;
int vis[maxn][maxn];
bool check(int S,int x)
{
    for(int i=0;i<N;i++)
    {
        if(!(S&(1<<i)))
        {
            if(vis[x][i])return false;
        }
    }
    int cnt=0;
    for(int i=0;i<N;i++)
    {
        if(vis[x][i])continue;
        if(S&(1<<i))
        {
            int cnt=0,j;
            for(j=i+1;j<M;j++)
            {
                if(S&(1<<j))cnt++;
                if(cnt==2)break;
            }
            if(cnt==2&&(S&(1<<j))&&!vis[x][j])return false;
        }
    }
    return true;
}
int sta[maxn];
bool check1(int now)
{
    if(now<2)return true;
    for(int i=0;i<M;i++)
    {
        if(vis[now][i])continue;
        if(sta[now]&(1<<i))
        {
            int cnt=0,j;
            for(j=now-1;j>=0;j--)
            {
                if(sta[j]&(1<<i))cnt++;
                if(cnt==2)break;
            }
            if(cnt==2&&(sta[j]&(1<<i))&&!vis[j][i])return false;
        }
    }
    return true;
}
void dfs(int now,int lastS,int sum)
{
    if(now>=N)
    {
        ans=max(ans,sum-Q);
        return;
    }
    for(int i=0;i<S;i++)
    {
        if(!check(i,now))continue;
        sta[now]=i;
        if(!check1(now))continue;
        int num=0;
        for(int j=0;j<M;j++)
            if((i&(1<<j)))num++;
        dfs(now+1,i,sum+num);
    }
}
int main()
{
    while(scanf("%d%d%d",&N,&M,&Q)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=Q;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            vis[x][y]=1;
        }
        S=(1<<M);
        ans=0;
        for(int i=0;i<S;i++)
        {
            if(!check(i,0))continue;
            int num=0;
            for(int j=0;j<M;j++)
                if(i&(1<<j))num++;
            sta[0]=i;
            dfs(1,i,num);
        }
        printf("%d\n",ans);
    }
    return 0;
}


Play Game

Problem Description

Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from either pile, and the score of the card will be added to his total score. Alice and Bob are both clever enough, and will pick up cards to get as many scores as possible. Do you know how many scores can Alice get if he picks up first?

Input

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

Each case contains 3 lines. The first line is the N (N≤20). The second line contains N integer ai (1≤ai≤10000). The third line contains N integer bi (1≤bi≤10000).

Output

For each case, output an integer, indicating the most score Alice can get.

Sample Input

2

1

23

53

3

10 100 20

2 4 3

Sample Output

53

105

题意:两队数,每次可以从首尾选一个,两个人轮流选,都足够聪明,问第一个人最多能选多少

思路:dp记忆话搜索,dp[al][ar][bl][br]表示两个队列在当前状态下,能得到的最大值,因为两个人轮流取,所以要用总和减去

[code]#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=25;
int dp[maxn][maxn][maxn][maxn];
int a[maxn],b[maxn];
int N;
int DP(int al,int ar,int bl,int br,int sum)
{
    if(al>ar&&bl>br)return 0;
    if(dp[al][ar][bl][br]!=-1)
        return dp[al][ar][bl][br];
    int ans=0;
    if(al<=ar)
    {
        int tmp=0;
        if(al<=N)tmp=max(sum-a[al]-DP(al+1,ar,bl,br,sum-a[al])+a[al],tmp);
        if(ar>=1)tmp=max(tmp,sum-a[ar]-DP(al,ar-1,bl,br,sum-a[ar])+a[ar]);
        ans=max(tmp,ans);
    }
    if(bl<=br)
    {
        int tmp=0;
        if(bl<=N)tmp=max(sum-b[bl]-DP(al,ar,bl+1,br,sum-b[bl])+b[bl],tmp);
        if(br>=1)tmp=max(tmp,sum-b[br]-DP(al,ar,bl,br-1,sum-b[br])+b[br]);
        ans=max(tmp,ans);
    }

    if(ans>dp[al][ar][bl][br])dp[al][ar][bl][br]=ans;
    return dp[al][ar][bl][br];
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        int sum=0;
        for(int i=1;i<=N;i++)scanf("%d",&a[i]),sum+=a[i];
        for(int i=1;i<=N;i++)scanf("%d",&b[i]),sum+=b[i];
        memset(dp,-1,sizeof(dp));
        printf("%d\n",DP(1,N,1,N,sum));
    }
    return 0;
}


Difference

Problem Description

A graph is a difference if every vertex vi can be assigned a real number ai and there exists a positive real number T such that

(a) |ai| < T for all i and

(b) (vi, vj) in E <=> |ai - aj| >= T,

where E is the set of the edges.

Now given a graph, please recognize it whether it is a difference.

Input

The first line of input contains one integer TC(1<=TC<=25), the number of test cases.

Then TC test cases follow. For each test case, the first line contains one integer N(1<=N<=300), the number of vertexes in the graph. Then N lines follow, each of the N line contains a string of length N. The j-th character in the i-th line is “1” if (vi, vj) in E, and it is “0” otherwise. The i-th character in the i-th line will be always “0”. It is guaranteed that the j-th character in the i-th line will be the same as the i-th character in the j-th line.

Output

For each test case, output a string in one line. Output “Yes” if the graph is a difference, and “No” if it is not a difference.

Sample Input

3

4

0011

0001

1000

1100

4

0111

1001

1001

1110

3

000

000

000

Sample Output

Yes

No

Yes

Hint

In sample 1, it can let T=3 and a[sub]1[/sub]=-2, a[sub]2[/sub]=-1, a[sub]3[/sub]=1, a[sub]4[/sub]=2.

这个题的题意没太说明白,除了要求有边的节点之间>=T,还要求没边的之间

[code]#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=310;
const int SIG=400;
const int INF=0x3f3f3f3f;
char s[maxn][maxn];
int N,tot;
vector<int> g[maxn];
int vis[maxn],dis[maxn];
int head[maxn],cnt[maxn];
bool dfs(int u)
{
    int len=g[u].size();
    for(int i=0;i<len;i++)
    {
        int v=g[u][i];
        if(!vis[v])
        {
            vis[v]=3-vis[u];
            if(dfs(v))return true;
        }
        else if(vis[v]==vis[u])
            return true;
    }
    return false;
}
struct node
{
    int v,next,f;
}edge[maxn*maxn*2];
void add_edge(int x,int y,int f)
{
    edge[tot].v=y;
    edge[tot].next=head[x];
    edge[tot].f=f;
    head[x]=tot++;
}
bool SPFA(int st)
{
    for(int i=0;i<=N;i++)
    {
        dis[i]=INF;
        cnt[i]=0;
        vis[i]=0;
    }
    vis[st]=1;
    dis[st]=0;
    queue<int> q;
    q.push(st);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].f;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                    if(++cnt[v]>N)
                        return true;
                }
            }
        }
    }
    return false;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        for(int i=0;i<N;i++)
        {
            scanf("%s",s[i]);
            g[i].clear();
        }
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
                if(s[i][j]=='1')g[i].push_back(j);
        memset(vis,0,sizeof(vis));
        bool flag=true;
        for(int i=0;i<N;i++)
        {
            if(vis[i])continue;
            vis[i]=1;
            if(dfs(i))
            {
                flag=false;
                break;
            }
        }
        if(!flag){printf("No\n");continue;}
        memset(head,-1,sizeof(head));
        tot=0;
        for(int i=0;i<N;i++)
        {
            for(int j=i+1;j<N;j++)
            {
                if(s[i][j]=='1')
                {
                    if(vis[i]==1)
                        add_edge(j,i,-SIG);
                    else add_edge(i,j,-SIG);
                }
                else
                {
                    if(vis[i]==1)add_edge(i,j,SIG-1);
                    else add_edge(j,i,SIG-1);
                }
            }
        }
        if(SPFA(0))printf("No\n");
        else printf("Yes\n");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: