1. 程式人生 > >2017 ACM-ICPC 亞洲區(烏魯木齊賽區)網路賽

2017 ACM-ICPC 亞洲區(烏魯木齊賽區)網路賽

For each test case, output all the pairs x, y that the x-the monkey can accept at leastone type of bananas from the y-th place.

These pairs should be outputted as ascending order. That is say that a pair of x, ywhich owns a smaller x should be output first.

If two pairs own the same x, output the one who has a smaller

y first.And there should be an empty line after each test case. 

題意:

已知每個猴子對應香蕉種類,每個香蕉種類對應供應商。

求猴子對應的供應商。

#include <vector>
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const int maxn =55;
int mp[maxn][maxn];
int hou[maxn][maxn];
int h[maxn],ban[maxn];
void init()
{
    memset(mp,0,sizeof mp);
    memset(hou,0,sizeof hou);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%d %d",&h[i],&ban[i]);
        for(int i=1;i<=m;i++)
        {
            int a,b;scanf("%d %d",&a,&b);//香蕉 店
            mp[a][b]=1;
        }
        for(int i=1;i<=n;i++)
        {
            int ho=h[i];
            int b=ban[i];
            for(int j=1;j<=50;j++)
            {
                if(mp[b][j])
                {
                    hou[ho][j]=1;
                }
            }
        }
        for(int i=1;i<=50;i++)
        {
            for(int j=1;j<=50;j++)
            {
                if(hou[i][j]==1)
                {
                    printf("%d %d\n",i,j);
                }
            }
        }
        printf("\n");
    }
    return 0;
}


C: Coconut

time limit

200ms

memory limit

131072KB
Coconut is Captain Gangplank's favourite fruit. That is why he needs to drink coconut

juice from b coconuts each day.

On his next trip, he would pass through N citis.

His trip would begin in the 1-st city and end in the N-th city.

The journey from the

i-th city to the (i + 1)-th city costs Di days.

Initially, there is no coconut on his ship. Fortunately, he could get supply of Ci coconutsfrom the i-th city.

Could you tell him, whether he could drink coconut juice every day during the trip no not?

Input Format

The first line contains an integer T , indicating that there are T test cases.
For each test case the first line contains two integers
N and b as described above.The second line contains N integers C1 , C2 , ⋯ , CN .
The third line contains
N − 1 integers D1 , D2 , ⋯ , DN −1 .
All integers in the input are less than
1000.

Output Format

For each case, output Yes if Captain Gangplank could drink coconut juice every day,and otherwise output No. 


題意:

從1按順序走到n,給你每條路用的時間,和每天要喝的數量,每座城市有的飲料數。

判斷中途會不會喝光,喝光no。

#include <vector>
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const int maxn =1100;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,b;
        scanf("%d %d",&n,&b);
        int c[maxn];
        int d[maxn];
        for(int i=1;i<=n;i++) scanf("%d",&c[i]);
        for(int i=1;i<n;i++) scanf("%d",&d[i]);
        int now=c[1];
        int flag=1;
        for(int i=1;i<n;i++)
        {
            if(now<d[i]*b)
            {
                flag=0;
                break;
            }
            else
                now=now-d[i]*b+c[i+1];
        }
        if(flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

F: Islands

time limit

1000ms

memory limit

131072KB

On the mysterious continent of Tamriel, there is a great empire founded by human.

To develope the trade, the East Empire Company is set up to transport goods fromplace to place.

Recently, the company wants to start their business in Solstheim, which is consists of Nislands.

Luckily, there are already M sea routes.
All routes are one-way, and the
i-th route can transport person and goods from island ui

to vi.
Now, the company nominates you a particular job to plan some new routes to make

sure that person and goods can be transported between any two islands.

Furthermore, because the neighboring regions are under attack by an increasingnumber of dragons, limited resources can be used to set up new routes.

So you should plan to build new routes as few as possible.

Input Format

The first line contains an integer T , indicating that there are T test cases.For each test case, the first line includes two integers N (N ≤ 10000) and

M (M ≤ 100000), as described above.
After that there are
M lines. Each line contains two integers ui and vi.

Output Format

For each test case output one integer, represent the least number of routes required tonew. 


題意:

給你n個點和m條邊,求出在新增幾條邊變成強連通。

point:

tarjan演算法。

#include <vector>
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <stack>
#include <string.h>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const int maxn = 10000 + 10;
const int maxm = 100000 + 10;
vector<int> G[maxn];
int T;
int n,m;
int cor[maxn],numcor,low[maxn],dfn[maxn],tm;
int inq[maxn];
stack<int> q;
void init()
{
    for(int i=0;i<=n;i++) G[i].clear();
    numcor=0;
    memset(cor,0,sizeof cor);
    memset(low,0,sizeof low);
    memset(dfn,0,sizeof dfn);
    memset(inq,0,sizeof inq);
    tm=0;
    while(!q.empty()) q.pop();
}
void tanjar(int u)
{
    dfn[u]=low[u]=++tm;
    q.push(u);
    inq[u]=1;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(dfn[v]==0)
        {
            tanjar(v);
            if(low[u]>low[v])
            {
                low[u]=low[v];
            }
        }
        else if(inq[v]==1&&low[u]>dfn[v])
        {
            low[u]=dfn[v];
        }
    }
    if(dfn[u]==low[u])
    {
        numcor++;
        int v;
        do
        {
            v=q.top();
            cor[v]=numcor;
            inq[v]=0;
            q.pop();
        }
        while(v!=u);
        
    }
}
int main()
{
    scanf("%d",&T);
    while(T--)//特判
    {
        scanf("%d %d",&n,&m);
        init();
        int uu[maxm],vv[maxm];
        for(int i=1;i<=m;i++)
        {
            int u,v;scanf("%d %d",&u,&v);
            G[u].push_back(v);
            uu[i]=u;
            vv[i]=v;
        }
        for(int i=1;i<=n;i++)
        {
            if(dfn[i]==0)
            {
                tanjar(i);
            }
        }
        int ru[maxn],chu[maxn];
        memset(ru,0,sizeof ru);
        memset(chu,0,sizeof chu);
        for(int i=1;i<=m;i++)
        {
            if(cor[vv[i]]==cor[uu[i]]) continue;
            ru[cor[vv[i]]]++;
            chu[cor[uu[i]]]++;
        }
        int ru0=0,chu0=0;
        int k=0;
        for(int i=1;i<=numcor;i++)
        {
            if(ru[i]==0&&chu[i]==0)
            {
               k++;
            }
            if(ru[i]==0) ru0++;
            if(chu[i]==0) chu0++;
        }
        if(k==1&&numcor==1) printf("0\n");
        else
        {
            printf("%d\n",max(ru0,chu0));
        }
    }
    
    return 0;
}


H: Skiing

time limit

1000ms

memory limit

131072KB

In this winter holiday, Bob has a plan for skiing at the mountain resort.

This ski resort has M different ski paths and N different flags situated at those turningpoints.

The i-th path from the Si-th flag to the Ti-th flag has length Li.
Each path must follow the principal of reduction of heights and the start point must be

higher than the end point strictly.

An available ski trail would start from a flag, passing through several flags along thepaths, and end at another flag.

Now, you should help Bob find the longest available ski trail in the ski resort.

Input Format

The first line contains an integer T , indicating that there are T cases.
In each test case, the first line contains two integers
N and M where 0 < N ≤ 10000

and 0 < M ≤ 100000 as described above.
Each of the following
M lines contains three integers Si, Ti, and Li (0 < Li < 1000)

describing a path in the ski resort.

Output Format

For each test case, ouput one integer representing the length of the longest ski trail. 


題意:

n個點,m條邊,求出能走的最長路。

point:

記憶化搜尋。隊友的程式碼。

#include <vector>
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const int maxn = 10000 + 10;
int n,m,t,vis[maxn];
struct node
{
    int next,dist;
    node(int a,int b){next = a,dist = b;}
};
vector<node>e[maxn];
int dfs(int x)
{
    if(vis[x]) return vis[x];
    for(int i=0;i<e[x].size();i++)
    {
        int nx = e[x][i].next,dis = e[x][i].dist;
        vis[x] = max(vis[x],dfs(nx)+dis);
    }
    return vis[x];
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        int ans = 0;
        memset(vis,0,sizeof vis);
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++) e[i].clear();
        for(int i=0;i<m;i++)
        {
            int u,v,dist;
            scanf("%d%d%d",&u,&v,&dist);
            e[u].push_back(node(v,dist));
        }
        for(int i=1;i<=n;i++) ans = max(ans,dfs(i));
        printf("%d\n",ans);
    }
    return 0;
}