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
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
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
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'.
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.
题解:用字符串的最小表示法表示各个字符串存在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
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'.
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.
题解:鸽巢原理的应用,至少有一个堆有+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;
}