Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int grid[45][45];
int main() {
int num_tests;
cin >> num_tests;
for (int test = 0; test < num_tests; ++test) {
int n, m;
cin >> n >> m;
int max_i = 0, max_j = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
cin >> grid[i][j];
if (grid[i][j] > grid[max_i][max_j])
max_i = i, max_j = j;
}
int h = max(max_i+1, n-max_i);
int w = max(max_j+1, m-max_j);
cout << h * w << '\n';
}
}
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int num_tests;
cin >> num_tests;
for (int test = 0; test < num_tests; ++test) {
int n; cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
if (n % 2 == 1) {
cout << "Mike\n";
continue;
}
int smallest = 0;
for (int i = 0; i < n; ++i)
if (a[i] < a[smallest])
smallest = i;
if (smallest % 2 == 0) cout << "Joe\n";
else cout << "Mike\n";
}
}
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define N 1010
int grid[N][N], mn[N][N], mx[N][N];
int main() {
int num_tests;
cin >> num_tests;
for (int test = 0; test < num_tests; ++test) {
int n, m;
cin >> n >> m;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
cin >> grid[i][j];
mn[0][0] = mx[0][0] = grid[0][0];
for(int i = 1; i < n; ++i)
mx[i][0] = mn[i][0] = mx[i - 1][0] + grid[i][0];
for(int j = 1; j < m; ++j)
mx[0][j] = mn[0][j] = mx[0][j - 1] + grid[0][j];
for(int i = 1; i < n; ++i)
for(int j = 1; j < m; ++j) {
mx[i][j] = max(mx[i - 1][j], mx[i][j - 1]) + grid[i][j];
mn[i][j] = min(mn[i - 1][j], mn[i][j - 1]) + grid[i][j];
}
if(mx[n - 1][m - 1] % 2 || mn[n - 1][m - 1] > 0 || mx[n - 1][m - 1] < 0)
cout << "NO\n";
else
cout << "YES\n";
}
}
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define N 2010
vector<int> tree[N];
int dfs(int i, int p) {
int sm = 0, z = 0;
for (int j : tree[i]) if (j != p) {
int x = dfs(j, i);
sm += x;
if (x == 0)
z++;
}
return sm + max(0, z - 1);
}
int main() {
int num_tests;
cin >> num_tests;
for (int test = 0; test < num_tests; ++test) {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
if (n == 1)
cout << "0\n";
else {
int ans = n;
for (int i = 1; i <= n; ++i)
ans = min(ans, 1 + dfs(i, i));
cout << ans << '\n';
}
for (int i = 1; i <= n; ++i)
tree[i].clear();
}
}
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define N 200010
vector<int> tree[N];
int dfs(int i, int p) {
int sm = 0, z = 0;
for (int j : tree[i]) if (j != p) {
int x = dfs(j, i);
sm += x;
if (x == 0)
z++;
}
return sm + max(0, z - 1);
}
int main() {
int num_tests;
cin >> num_tests;
for (int test = 0; test < num_tests; ++test) {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
int max_deg = 0;
for (int i = 1; i <= n; ++i)
max_deg = max(max_deg, (int)tree[i].size());
if (max_deg == 0)
cout << "0\n";
else if (max_deg < 3)
cout << "1\n";
else {
for (int i = 1; i <= n; ++i)
if (tree[i].size() >= 3) {
cout << dfs(i, i) << '\n';
break;
}
}
for (int i = 1; i <= n; ++i)
tree[i].clear();
}
}
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define N 600010
vector<pair<int, int>> graph[N];
vector<int> lst;
int ans[2][N];
bool used[N], usedEdges[N];
void dfs(int i) {
lst.push_back(i);
if (!used[i]) {
used[i] = true;
for (pair<int, int> p : graph[i])
if (!usedEdges[p.second]) {
usedEdges[p.second] = true;
dfs(p.first);
lst.push_back(i);
}
}
}
int main() {
int n; cin >> n;
string ptop(n, 'U'), pbot(n, 'D');
string mtop(n, 'U'), mbot(n, 'D');
for (int i = 0; i < n; ++i) {
int u, v;
cin >> u >> v;
graph[u].emplace_back(v, i);
graph[v].emplace_back(u, i);
}
int idx = 0;
for (int i = 1; i <= 2 * n; ++i) if (!used[i]) {
dfs(i);
lst.pop_back();
int k = lst.size() / 2;
if (k == 1) {
cout << "-1\n";
exit(0);
}
for (int j = 0; j < k; ++j) {
ans[0][j + idx] = lst[j];
ans[1][j + idx] = lst[2 * k - 1 - j];
}
for (int j = 0; j < k - 1; j += 2)
ptop[j + idx] = pbot[j + idx] = 'L', ptop[j + 1 + idx] = pbot[j + 1 + idx] = 'R';
for (int j = 1; j < k - 1; j += 2)
mtop[j + idx] = mbot[j + idx] = 'L', mtop[j + 1 + idx] = mbot[j + 1 + idx] = 'R';
lst.clear();
idx += k;
}
cout << "2 " << n << '\n';
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < n; ++j)
cout << ans[i][j] << ' ';
cout << '\n';
}
cout << ptop << '\n' << pbot << '\n';
cout << mtop << '\n' << mbot << '\n';
}