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
AC: 根据测试用例,最后一个测试用例的值爆int,所以用long long
#include
#include
#include
#include
#include
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
题解:用字符串的最小表示法表示各个字符串存在Map里,然后查询即可
#include
#include
#include
#include
#include
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
Output
For each set of data, output a string. If Alice's assertion is correct, output 'Yes'; otherwise, output 'No'.
Sample Input
Sample Output
题解:鸽巢原理的应用,至少有一个堆有+1个元素。
#include
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;
}