NC month title race up

NC month title race up

B- tree daughter strand:

Title Description
Given a tree T, each point on the tree T has a weight.
Define the size of the sub-tree of the chain is: and the weights of all the nodes on the daughter strand.
Please find a sub-largest chain in the tree T and output.
Input Description:
The first line input a n, 1≤n≤10 ^ 5.

The next line contains the number n, the number for each ai, -10 ^ 5 <= ai <= 10 ^ 5, the value of i represents the weight of the node.
Then there are n-1 rows, each containing two numbers u, v (1 <= u , v <= n, u! = V), indicates there is an edge between u and v.
Output Description: The
only contain a number that represents the answer we need.
Example 1

Input
. 5
2. 3 -2 -1 -1
. 1 2
2. 3
2. 4
2. 5

Output
4
illustrate
examples maximum daughter strand 1 -> 2 -> 5
Notes:
a node may be referred to as a chain

ps: I Sike giant pit of long long type, not of graph theory, learning quickly drawing a wave of deposit talk about it, but still thank the patience guidance of a giant man. (After the summary graph theory) diameter tree this problem and similar ...

AC:


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 7;
const int maxm = 2e5 + 7;
const long long mod = 1e9 + 7;
const long long INF = 0x3f3f3f3f;
LL n,a[maxn],u,v,dp[maxn],ans=-INF;
int vis[maxn];
vector<LL> G[maxn];

void dfs(LL x)
{
    vis[x] = 1; //标记该点已经走过
    LL tmp1 = a[x],tmp2 = a[x]; //tmp1为该节点最长链,tmp2为该节点次长链 
    for(int i = 0; i<G[x].size(); i++)
    {
         if(vis[G[x][i]]) //判断邻接点是否遍历过 
            continue;
         dfs(G[x][i]); //如果未遍历过就先进行遍历
         dp[x] = max(dp[x],dp[G[x][i]] + a[x]); //用dp来存当前该根节点最大权值
         if(dp[G[x][i]] + a[x] > tmp1) //判断是否新的子链大于最长子链 
         {
            tmp2 = tmp1; //将原来的最长子链变成第二长子链
            tmp1 = dp[G[x][i]] + a[x]; 
         } 
         else if(dp[G[x][i]] + a[x] > tmp2) //判断是否新的子链大于次长子链而不是最长子链
            tmp2 = dp[G[x][i]] + a[x]; //更新次长子链 
    } 
    dp[x] = tmp1; //仅有一一个子节点 
    ans = max(ans,tmp1 + tmp2 -a[x]);//最长子链 + 次长子链 - 当前节点权值 
}

int main()
{
    scanf("%lld",&n);
    for(int i = 1; i <= n; i++)
        scanf("%lld",&a[i]);
    for(int i = 1; i < n; i++)
    {
        scanf("%lld%lld",&u,&v);
        G[u].push_back(v); //用邻接表的方式存图 
        G[v].push_back(u);
    }
    memset(vis,0,sizeof(vis));
    dfs(1); //从第一个点开始遍历整个图 
    printf("%lld",ans);
    return 0;
}

D- collection of paper:

Title Description
After we put the room modeled in accordance with Cartesian coordinate system, each point will have a coordinate.
Suppose now house some pieces of paper that needs to be collected, you collect pieces of paper you have to return to the original position, you need to develop a strategy to make the shortest walking distance himself.
You can only move along the x-axis or y-axis direction from the position (i, j) moves to a position adjacent (i + 1, j), (i-1, j), (i, j + 1) or ( i, j-1) an increase in distance.

Input Description:
given a T in the first row, 1≤T≤10, represents the number of test cases.
For each input, given the size of the room in the first row, the second row gives you an initial position.
Next is given a positive integer n, the number of sheets representative of 1≤n≤10.
Next n lines of ordinate represents a position of the paper sheet.
Ensure room is less than 20 × 20, the paper sheet is located within a certain room.
Description Output:
For each input and output line answer.
See sample format.
Example 1
Input

. 1
10 10
. 1. 1
. 4
2. 3
. 5. 5
. 9. 4
. 6. 5
Output

The shortest path has length 24

AC:


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+7;
const int maxm=2e5+7;
const int num=15;
const long long mod=1e9+7;
const long long INF=0x3f3f3f3f;
int minLen=INF;
int a,b,n;
int r,c,t;
int vis[num];
struct node
{
    int x,y;    
}pos[num];


void dfs(int x,int y,int nn,int step)
{
    if(!nn)
    {
        step+=abs(x-r)+abs(y-c);
        minLen=min(minLen,step);
        return;
    }
    for(int k=1;k<=n;k++)
    {
        int xx=pos[k].x;
        int yy=pos[k].y;
        if(!vis[k])
        {
            step+=abs(xx-x)+abs(yy-y);
            vis[k]=1;
            dfs(xx,yy,nn-1,step);
            step-=abs(xx-x)+abs(yy-y);
            vis[k]=0;
        }
    }
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d%d",&a,&b,&r,&c,&n);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&pos[i].x,&pos[i].y);
        memset(vis,0,sizeof(vis));
        dfs(r,c,n,0);
        if(minLen<INF)
            printf("The shortest path has length %d\n",minLen);
        else
            printf("-1\n");
    }
    return 0;
}

E- box coloring (water issues)

Description Title
of a rectangular region is divided into small squares for n rows and m columns, the initial these boxes are not colored.
In order to make the rectangle appear not a single color, the now selected row r and column c lattice grid selected color.
Please complete the calculation of color, the number of lattice uncolored.
Input Description:
multiple sets of input
each set of four digital inputs are given in a row n, m, r, c, the title meanings as shown.
Ensure data 1 <= n, m <= 10 ^ 6, 1 <= r <= n, 1 <= c <= m.
Output Description:
each input and output row for the answer.
Example 1
Input
5523

Output
6

AC:

#include<bits/stdc++.h>
using namespace std;
long long n,m,r,c;
int main()
{
    while(~scanf("%lld%lld%lld%lld",&n,&m,&r,&c))
    {
         printf("%lld\n",n*m-m*r-n*c+r*c);   
    }
    return 0;
}

F- multiplicative digital (or water title)

Title description
we will know that a number greater than 1 is multiplied by a number greater than the other will be greater than the product of any one multiplier.
Now given two numbers n, d, you can calculate the n-times multiplying the result by 100. d.
Input Description:
multiple sets of input
each input line in the given n, d, 1≤n, d≤100.
Output Description:
input and output row for each answer.
Example 1
input
. 5 1
. 11 1
85 2

Output
500
1100
850000, China

AC:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+7;
const int maxm=2e5+7;
const long long inf=0x3f3f3f3f;
const long long mod=1e9+7;
string n;
char a='0';
int d;
int main()
{
    while(cin>>n>>d)
    {
        for(int i=0;i<d;i++)
        {
            n=n+a;  
            n=n+a;
        }
    cout<<n<<endl;
    }
    return 0;
}


//题解:

#include <bits/stdc++.h>
 
using namespace std;
 
int main(){
    int n, d;
    while (cin >> n >> d){
        cout << n;
        for (int i = 1;i <= d; cout << "00", i++);
        cout << endl;
    }
    return 0;
}

G- warehouse location (violence enumeration)

Title:
Cattle were able to have a fixed demand in a small town, in order to save shipping costs, he decided to build a warehouse in a small town, but he did not know where to choose, you can minimize costs.
Gives a m × n matrix, representing the number of times each year, the small town the position of the goods demand. We spend as defined distance truck cargo transport, trucks can only travel along the horizontal or vertical direction.

Input Description:
first inputs T, T≤10, test data representative of the number of groups in a row.
Each set of two positive integers n input given in the first row, m, 1≤n, m≤100, representing the width and height of the matrix.
Next m lines of n-th number not more than 1000, in behalf of the matrix elements.
Output Description:
each output line in the input answer.
Example 1
Input
. 3
2 2
1 1
1 0
. 4. 4
0. 8 2 0
1. 4. 5 0
0 1 0 1
. 3. 9 2 0
. 6. 7
0 0 0 0 0 0
0 1 0. 3 0 1
2. 9 1 2 1 2
. 8. 7 . 3. 1. 4. 3
. 1. 7. 7 2 2 0
0. 1. 1 0 0 0
0 0 0 0 0 0

Output
2
55
162

Note:
the delivery time can only be a single transportation, if the position requires three times, the truck must run three times.
Even if the position needs to be delivery, we can still choose this location as a warehouse.

AC:

//O(n^2 * m^2)也能过...
#include<bits/stdc++.h>
using namespace std;
const int num=105;
int maze[num][num];
int t,n,m,ans;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        ans=0x3f3f3f3f;
        scanf("%d%d",&m,&n);
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= m;j++)
                scanf("%d",&maze[i][j]);
        for(int x = 1;x <= n;x++)
        {
            for(int y = 1;y <= m;y++)
            {
                int sum = 0;
                for(int i = 1;i <= n;i++)
                {
                    for(int j = 1;j <= m;j++)
                    {
                        sum += (abs(x - i) + abs(y - j)) * maze[i][j];
                    }
                }
                ans = min(sum,ans);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

As well as the rest of the slowly come ~ ~ ~

Guess you like

Origin www.cnblogs.com/K2MnO4/p/12359765.html