contents:
A. Make Even
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int, int>
#define psi pair<string, int>
#define ull unsigned ll
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define ld long double
const int N = 1E5 + 7;
#define INF ~0ULL
int main()
{
int t;
cin >> t;
int n;
while (t--)
{
cin >> n;
if (n % 2 == 0)
{
cout << 0 << endl;
}
else if (n < 10 && n % 2 == 1)
{
cout << -1 << endl;
}
else
{
bool flag = 0;
string s = to_string(n);
int len = s.size();
for (int i = 0; i < len; i++)
{
if ((s[i] - '0') % 2 == 0)
{
flag = 1;
}
}
if (flag == 0)
{
cout << -1 << endl;
}
else if ((s[0] - '0') % 2 == 0)
{
cout << 1 << endl;
}
else
{
cout << 2 << endl;
}
}
}
}
B. Team Composition: Programmers and Mathematicians
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int, int>
#define psi pair<string, int>
#define ull unsigned ll
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define ld long double
const int N = 1E5 + 7;
#define INF ~0ULL
int main()
{
int t;
cin >> t;
int a, b;
while (t--)
{
cin >> a >> b;
int ans;
ans = min(a, b);
ans = min((a + b) / 4, ans);
cout << ans << endl;
}
}
C. Polycarp Recovers the Permutation
#include <bits/stdc++.h>
std::mt19937 rnd(time(0));
#define FL fflush(stdout)
using namespace std;
int a[10000005];
int main()
{
int i,j,n,m,t;
cin>>t;
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
if(a[1]!=n&&a[n]!=n)puts("-1");
else if(a[1]==n){
printf("%d",n);
for(i=n;i>=2;i--)printf(" %d",a[i]);
printf("\n");
}
else {
for(i=n-1;i>=1;i--)printf("%d ",a[i]);
printf("%d\n",n);
}
}
return 0;
}
D. Weights Assignment For Tree Edges
以始为终
#include <bits/stdc++.h>
using namespace std;
int t;
int n;
vector<int> b, p, d, w;
void solve()
{
scanf("%d", &n);
b = vector<int>(n);
p = vector<int>(n);
d = vector<int>(n);
w = vector<int>(n);
for (int i = 0; i < n; i++)
{
scanf("%d", &b[i]);
b[i]--;
}
for (int i = 0; i < n; i++)
{
scanf("%d", &p[i]);
p[i]--;
d[p[i]] = i;
}
bool ok = 1;
for (int i = 0; i < n; i++)
{
w[i] = d[i] - d[b[i]];
if (w[i] < 0)
ok = 0;
}
if (!ok)
cout << -1 << endl;
else
{
for (int i = 0; i < n; i++)
cout << w[i] << " ";
cout << endl;
}
}
int main()
{
scanf("%d", &t);
while (t--)
{
solve();
}
}
E1. Escape The Maze (easy version)
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const int INF = 1e9;
int dp[200002], x[200002], top[200002], a, k, n, t;
vector<int> G[200002];
void dfs(int nod, int p, int dist)
{
if (x[nod])
top[nod] = dist;
else
top[nod] = 1e9;
if (G[nod].size() == 1 && nod > 1)
{
dp[nod] = (top[nod] < INF ? 1 : INF);
return;
}
for (auto it : G[nod])
{
if (it != p)
{
dfs(it, nod, dist + 1);
top[nod] = min(top[it], top[nod]);
dp[nod] = min(INF, dp[nod] + dp[it]);
}
}
if (top[nod] - dist <= dist)
dp[nod] = 1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--)
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
G[i].clear();
x[i] = 0;
top[i] = dp[i] = 0;
}
while (k--)
{
cin >> a;
x[a] = 1;
}
for (int i = 1; i < n; i++)
{
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, 0, 0);
if (dp[1] != INF)
{
cout << "NO\n";
}
else
cout << "YES\n";
}
return 0;
}