2023杭电多校(一)

1002 City Upgrading

类似题及其题解

City Upgrading

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 524288/131072 K (Java/Others)
Total Submission(s): 306    Accepted Submission(s): 78


 

Problem Description
The city where crazyzhk resides is structured as a tree. On a certain day, the city's network needs to be upgraded. To achieve this goal, routers need to be deployed. Each router covers the node it is placed on and its neighboring nodes. There is a cost ai associated with placing a router at each node. The question is: How can the routers be deployed at minimum cost to ensure that every node is covered?
 

Input
The input consists of multiple test cases. The first line contains a single integer t(1≤t≤1000) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers n (2≤n≤105) — the number of the vertices in the given tree.

The second line of each case are n integers ai(1≤ai≤105),denoting the cost of setting up a router at each node.

Each of the next n−1 lines contains two integers u and v (1≤u,v≤n, u≠v) meaning that there is an edge between vertices u and v in the tree.

The data guarantees that the sum of n will not exceed 2⋅105
 

Output
For each test case print a single integer ——the minimum cost to ensure that every node is covered
 

Sample Input
 
 
2 7 13 20 1 20 6 9 8 1 2 1 3 2 4 2 5 3 6 5 7 4 1 17 13 4 1 2 1 3 3 4
 

Sample Output
 
 
27 5

AC: 根据测试用例,最后一个测试用例的值爆int,所以用long long

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector> 
#include<map>
#include<string.h>
#include<stdio.h>
#include<cstdio>
using namespace std;

const int N = 1e5 + 5;

vector<int> v[N];

int val[N];
long long dp[N][3];
//dp[i][0] 自己覆盖自己
//dp[i][1] 儿子覆盖自己
//dp[i][2] 父亲覆盖自己

inline int read()
{
    int p = 0, f = 1; char c = getchar();
    while (c < '0' || c>'9') { if (c == '-')f = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { p = p * 10 + c - '0'; c = getchar(); }
    return f * p;
}

void dfs(int u, int f)
{
    dp[u][0] = val[u]; dp[u][2] = 0; dp[u][1] = 0;
    long long tep;
    int ct = 0;
    long long minsub = 1e9;
    for (vector<int>::iterator it = v[u].begin(); it != v[u].end(); it++)
    {
        int to = *it;
        if (*it == f) continue;
        dfs(to, u);
        tep = min(dp[to][0], dp[to][1]);
        dp[u][0] += min(tep, dp[to][2]);
        if (dp[to][0] < dp[to][1])  ct++;
        else minsub = min(minsub, dp[to][0] - dp[to][1]);
        dp[u][2] += tep;
        dp[u][1] += tep;
    }
    if (!ct) dp[u][1] += minsub;
}


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("D:\\1002.txt", "r", stdin);
	int t;
    t = read();
    while (t--)
    {
        int n;
        n = read();
        for (int i = 1; i <= n; i++)
        {
            val[i]=read();
            v[i].clear();
        }
        int x, y;
        for (int i = 1; i < n; i++)
        {
            x = read();
            y = read();
            v[x].push_back(y);
            v[y].push_back(x);
        }
        dfs(1, 0);
        cout << min(dp[1][0], dp[1][1])<<endl;

    }



}

1005 Cyclically Isomorphic

Cyclically Isomorphic

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 581    Accepted Submission(s): 129


 

Problem Description
If there exists an integer k such that string S becomes equal to string T after being **cyclically right-shifted** by k positions, then the strings S and T are said to be **cyclically right-shifted**.

Now, given n strings of length m consisting of **lowercase letters** , there are a total of Q queries. Each query provides two positive integers x and y. If the strings sx and sy are **cyclically right-shifted** , output 'Yes'; otherwise, output 'No'.
 

Input
The input consists of multiple test cases. The first line contains a single integer T(1≤T≤5) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers n and m (1≤n×m≤105)— the number of the strings and the length of strings.

Each of the next n lines contains a string of lowercase letters si。

The next line contains a positive integer Q (1≤Q≤105)。

Each of the next Q lines contains two integers x,y (1≤x,y≤n) asks whether the string sx and the string sy are cyclic isomorphic.  
 

Output
For each test case, output Q lines. Each line should contain a string indicating whether the current query strings sx and sy are cyclically isomorphic. If they are cyclically isomorphic, output 'Yes'; otherwise, output 'No'.
 

Sample Input
 
 
2 2 2 ab ba 1 1 2 4 3 aab baa bba bab 6 1 2 1 3 1 4 2 3 2 4 3 4
 

Sample Output
 
 
Yes Yes No No No No Yes
 

题解:用字符串的最小表示法表示各个字符串存在Map里,然后查询即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector> 
#include<map>
#include<string.h>
#include<stdio.h>
#include<cstdio>
using namespace std;
int n, m;
map<int, string> mp;
string minexpression(string s,int le)
{
	string st = s+s;
	string tp;
	for (int i = 0; i < le; i++)
	{
		tp = st.substr(i,  le);
		//cout<<"iii: " << tp << endl;
		if (s > tp) s = tp;
	}
	return s;
}
	string s1,s2;

int main()
{
//	freopen("D:\\1.txt", "r", stdin);
 
 ios::sync_with_stdio(false);
 cin.tie(0);
 cout.tie(0);
 
	int t;
	cin >> t;
	string sp;
	while (t--)
	{
		cin >> n >> m;
		for (int i = 1; i <= n; i++)
		{
			cin >> sp;
			mp[i] = minexpression(sp, m);
			//cout << "i: " << i << " " << sp << "  " << mp[i] << endl;
		}
		int q;
		cin >> q;
		int a, b;
	
		while (q--)
		{
			cin >> a >> b;
			if (mp[a] == mp[b])
			{
				//s1+="Yes";
			  cout << "Yes" << endl;
				}
			else
			
			{
			 //s1+="No";
			  cout << "No" << endl;
		}
		}
	}

}

1009 Assertion

Assertion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 185    Accepted Submission(s): 59


 

Problem Description
Alice boldly asserts to you that if you divide m items into n groups, there will definitely be one group with a quantity of items greater than or equal to d.

Due to Alice's excessive self-confidence, she is unaware that some of her assertions are actually incorrect. Your task is to determine whether Alice's assertion is correct. If Alice's assertion is true, output 'Yes'; otherwise, output 'No'.
 

Input
The input consists of multiple test cases. The first line contains a single integer T(1≤T≤105) — the number of test cases. Description of the test cases follows.

The first line of each test case contains three integers n,m,d (2≤m≤109,1≤n<m,0≤d≤109),n and m represent the number of groups and the quantity of items, respectively, in Alice's assertion. The symbol d signifies Alice's claim that there will always be at least one group with a quantity of items greater than or equal to d.
 

Output
For each set of data, output a string. If Alice's assertion is correct, output 'Yes'; otherwise, output 'No'.
 

Sample Input
 
 
3 1 2 1 2 3 2 3 10 4
 

Sample Output
 
 
Yes Yes Yes

题解:鸽巢原理的应用,至少有一个堆有\frac{\sqsubset m-1\sqsupset}{n}+1个元素。

#include <bits/stdc++.h>
 
using namespace std;
  
int main() {
	cin.tie(nullptr)->sync_with_stdio(0);
 
	int T;
	cin >> T;
 
	while (T--) {
		int n , m , x;
		cin >> n >> m >> x;
 
		int d = (m - 1) / n + 1;
		if (x <= d) {
			cout << "Yes\n";
		} else {
			cout << "No\n";
		}
	}
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linalw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值