Thanks for participating in our CodeNite 2025.
2120A — Square of Rectangles
Idea: Dragmon Solution: Dragmon Prepared by: Dragmon
There are only two possible ways to arrange rectangles into a square if possible.
The only cases possible to arrange rectangles into a square are:
- All three rectangles are put side by side, i.e. $$$l_1=l_2=l_3=b_1+b_2+b_3$$$ or $$$b_1=b_2=b_3=l_1+l_2+l_3$$$.
- Rectangles $$$2$$$ and $$$3$$$ are side by side with rectangle $$$1$$$ above it, i.e. $$$l_1+l_2=l_1+l_3=b_1=b_2+b_3$$$ or $$$b_1+b_2=b_1+b_3=l_1=l_2+l_3$$$.
Check both these cases, and if either is true, output YES, otherwise NO. Complexity is $$$O(1)$$$ per test.
//Written By Aryan Sanghi
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll t;
cin>>t;
while(t--)
{
ll l1, b1, l2, b2, l3, b3;
cin>>l1>>b1>>l2>>b2>>l3>>b3;
if(l1+l2+l3 == b1 && b1==b2 && b2==b3) cout<<"YES\n";
else if(l2+l3 == l1 && b2==b3 && b1+b2==l1) cout<<"YES\n";
else if(b1+b2+b3 == l1 && l1==l2 && l2==l3) cout<<"YES\n";
else if(b2+b3 == b1 && l2==l3 && l1+l2==b1) cout<<"YES\n";
else cout<<"NO\n";
}
}
#include <iostream>
using namespace std;
int main(){
int t;
cin >> t;
int l1, b1, l2, b2, l3, b3;
auto check = [&] () {
if (l1 == l2 && l2 == l3) return (l1 == b1 + b2 + b3 || (b1 == b2 + b3 && 2*l1 == b1));
if (l2 == l3) return (b2 + b3 == b1 && b1 == l2 + l1);
return false;
};
while(t--) {
cin >> l1 >> b1 >> l2 >> b2 >> l3 >> b3;
if(check()) cout << "YES\n";
else {
swap(l1, b1); swap(l2, b2); swap(l3, b3);
if(check()) cout << "YES\n";
else cout << "NO\n";
}
}
return 0;
}
2120B — Square Pool
Idea: harshith_04, Dragmon Solution: harshith_04, Dragmon Prepared by: harshith_04
What happens to the balls that collide with an edge, eventually?
How does a collision between two balls affect the outcome?
Observations
- Any ball on the diagonals of the square that is shot towards a pocket will be potted on a free table.
- Any ball that collides with an edge will be in a $$$4$$$-periodic path colliding with the $$$4$$$ edges forever on a free table.
- The collisions are elastic, so if two balls collide, they exchange their directions.
- $$$\bigstar$$$ Neither collisions affect the effective number of balls traversing towards a pocket.
Therefore, the answer is the number of balls initially on the diagonals of the square and shot towards the pockets.
Hope you liked the figures; a lot of effort went into them.
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
int n, s, ans = 0, dxi, dyi, xi, yi;
while(t--) {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> dxi >> dyi >> xi >> yi;
if (dxi == dyi) ans += (xi == yi);
else ans += (xi + yi == s);
}
cout << ans << '\n';
ans = 0;
}
return 0;
}
2120C — Divine Tree
Idea: harshith_04 Solution: harshith_04 Prepared by: harshith_04
What are bounds on $$$m$$$ for a given $$$n$$$ to have a divine tree? And how does the tree look for the lower bound and the upper bound?
The $$$\text{min}$$$ and $$$\text{max}$$$ value of $$$m$$$ for a divine tree to exist are $$$n$$$ and $$$\frac{n \cdot (n + 1)}{2}$$$ respectively.
$$$\text{POC:}$$$ Any $$$m \in [\text{min}, \text{max}]$$$ can be achieved similar to exhaustive subset sum with redraws enabled.
Let $$$p = m - n$$$, $$$\text{cur} = 0$$$, $$$\text{ans}$$$ = [ ]. Now, we need to select a multiset of $$$n$$$ non-negative integers that sum to $$$p$$$.
$$$\text{Greedy:}$$$ Loop $$$j$$$ from $$$n - 1$$$ to $$$0$$$, if $$$\text{cur} + i \le p$$$: $$$\text{cur}$$$ += $$$i,$$$ $$$\text{ans.push_back}(i + 1)$$$
If $$$\text{ans.back}()$$$ is not $$$1$$$ add a $$$1$$$, why?
$$$\text{Construction:}$$$ The tree can always be simple path, why?
- Tree is rooted at $$$\text{ans}[0]$$$. Let's store $$$\text{vis}[0:n - 1] = \text{false}$$$ to keep track if a node is visited or not.
- Loop $$$i$$$ from $$$1$$$ to $$$\text{size(ans)}$$$ and add edge between $$$\text{ans}[i-1]$$$ to $$$\text{ans}[i]$$$ and mark all of them $$$\text{true}$$$ in $$$\text{vis}$$$.
- Take all un-visited in the array $$$\text{unvis}$$$ and add an edge from $$$1$$$ to $$$\text{unvis}[0]$$$.
- Loop $$$i$$$ from $$$1$$$ to $$$\text{size(unvis)}$$$ and add an edge from $$$\text{unvis}[i-1]$$$ to $$$\text{unvis}[i]$$$.
The divine tree which is a simple path looks like $$$\text{ans}[0] \leftrightarrow$$$ . . . $$$\leftrightarrow 1 \leftrightarrow \text{unvis}[0] \leftrightarrow$$$ . . . $$$\leftrightarrow \text{unvis.back}()$$$.
#include <iostream>
#include <cstdint>
#include <cassert>
#include <vector>
using namespace std;
#define i64 int64_t
void solve() {
i64 n, sum;
cin >> n >> sum;
if(sum < n || sum > n * (n + 1) / 2) {
cout << "-1\n";
return;
}
i64 k = sum - n;
vector<i64> ans;
i64 curr = 0, nsum = 0;
for(i64 i = n - 1; i >= 0; --i) {
if(curr == k) break;
if(curr + i <= k) {
curr += i;
ans.push_back(i + 1);
nsum += i + 1;
}
}
i64 ct = ans.size();
for(i64 i = 0; i < n - ct; ++i) ans.push_back(1);
nsum += (n - ct);
assert(nsum == sum);
if(n == sum) {
cout << "1\n";
for(i64 i = 1; i < n; i++) cout << i << ' ' << i + 1 << '\n';
return;
}
vector<bool> vis(n + 1, 1);
cout << ans[0] << '\n';
vis[ans[0]] = 0;
for(i64 i = 1; i <= n; i++) {
cout << ans[i - 1] << ' ' << ans[i] << '\n';
vis[ans[i - 1]] = 0, vis[ans[i]] = 0;
if(ans[i] == 1) {
i64 prev = 1;
for(i64 j = 2; j <= n; ++j) {
if(vis[j]) {
cout << prev << ' ' << j << '\n';
prev = j;
}
}
return;
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
i64 t;
cin >> t;
while(t--) solve();
return 0;
}
2120D — Matrix Game
Idea: Dragmon Solution: Dragmon Prepared by: Dragmon
Use pigeonhole principle to get minimum $$$n$$$ and then minimum $$$m$$$.
If each column is of size $$$n=k(a-1)+1$$$, by pigeonhole principle, it will have at least $$$a$$$ elements with the same value. Let those elements appear positions $$$p_1 \lt p_2 \lt ... \lt p_a$$$($$$p_i$$$ is the row number where the element occurs) and let the value be $$$v$$$. Consider the tuple $$$(v, p_1,p_2,...,p_a)$$$. If the same tuple appears $$$b$$$ times, we are done as we obtain an $$$a*b$$$ submatrix with all elements of same value. The number of possible values of the tuple is $$$k\times\,^nC_a$$$. So, there should be atleast $$$m=(b-1)k\times\,^nC_a+1$$$ columns for there to be $$$b$$$ repetitions by pigeonhole principle.
//Written By Aryan Sanghi
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
int modpower(int a, int b)
{
int r=1;
a=a%mod;
while(b>0)
{
if(b%2==1)
{
r*=a;
r%=mod;
}
b=b/2;
a*=a;
a%=mod;
}
return r;
}
int32_t main(){
int t;
cin>>t;
while(t--){
int a, b, k;
cin>>a>>b>>k;
int n=1, m=1;
n*=a-1;
n%=mod;
n*=k;
n%=mod;
n+=1;
n%=mod;
m*=b-1;
m%=mod;
m*=k;
m%=mod;
for(int i=0;i<a;i++){
m*=(n-i+mod)%mod;
m%=mod;
m*=modpower((a-i+mod)%mod, mod-2)%mod;
m%=mod;
}
m+=1;
m%=mod;
cout<<n<<" "<<m<<"\n";
}
}
#include <bits/stdc++.h>
using namespace std;
const long long mod=1000000007;
long long inv[100001];
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int T;
long long i,a,b,k,d,ans;
inv[1]=1;
for(i=2;i<=100000;i++)inv[i]=(mod-mod/i)*inv[mod%i]%mod;
for(cin>>T;T>0;T--)
{
cin>>a>>b>>k;
d=k*a-k+1;
ans=k;
for(i=1;i<=a;i++)ans=ans*((d-i+1)%mod)%mod*inv[i]%mod;
cout<<d%mod<<' '<<(ans*b-ans+1+mod)%mod<<'\n';
}
return 0;
}
2120E — Lanes of Cars
Idea: Dragmon Solution: Dragmon Prepared by: picramide, Dragmon
Use binary search to find minimum number of cars in a lane after optimal number lane shifts.
Adjust cars afterwards such that minimum number of cars remains same as found in binary search, but angriness is minimized.
Observe the following(Let $$$1$$$ car shift lanes at a time):
- It is always optimal to shift the car at the back of a lane before shifting cars in front of it.
- The optimal condition is that in every iteration, the car shifts from the back of the lane with max cars to the back of the lane with minimum cars.
- If $$$(\text{cars in the max lane} - \text{cars in min lane}) \lt = K$$$, then it is not optimal to shift any cars as it will only increase the angriness.
- It doesn't matter when a car switches lane; it can switch at any time and the optimal answer remains the same.
For a value $$$v$$$, let
- $$$def(v)$$$ be number of cars required such that each lane has atleast $$$v$$$ cars, i.e. $$$def(v)=\sum_{i=1}^N max(0, v-a[i]))$$$
- $$$defs(v)$$$ be number of lanes with less than $$$v$$$ cars initially, i.e. $$$defs(v)=\sum_{i=1}^N (v-a[i] \gt 0)$$$.
- $$$exc(v)$$$ be number of cars to remove such that each lanes has atmost $$$v$$$ cars, i.e. $$$exc(v)=\sum_{i=1}^N max(0, a[i]-v)$$$
- $$$excs(v)$$$ be number of lanes with more than $$$v$$$ cars initially, i.e. $$$excs=\sum_{i=1}^N (a[i]-v \gt 0)$$$
Using binary search, find the maximum value of $$$v$$$ for which $$$exc(v+k) \gt def(v)$$$. This $$$v$$$ denotes the minimum value that the array will have after cars have switched lanes optimally, and the maximum value of the array will be $$$v+k$$$ if $$$exc(v+k)=def(v)$$$ and $$$v+k+1$$$ if $$$exc(v+k) \gt def(v)$$$.
Final sorted array $$$A'$$$ after optimal lane switches will have values between $$$v+1$$$ and $$$v+k-1$$$ remain the same as array $$$A$$$. Of the first $$$defs(v)$$$ elements, last $$$max(0, exc(v)-def(v)-excs(v))$$$ values will be $$$v+1$$$ and remaining values will be $$$v$$$. Of the last $$$excs(v)$$$ elements, last $$$min(excs(v), exc(v)-def(v))$$$ values will be $$$v+k+1$$$ and remaining elements will be $$$v+k$$$. Find minimum angriness after optimal lane switches using array $$$A'$$$ and calculating number of cars that have switched lanes. Time complexity is $$$O(n \log \max(A_i))$$$
Special thanks to picramide for the initial problem idea, which I misheard and it turned into this problem.
//Written By Aryan Sanghi
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for(ll i = 0; i < n;i++)
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n, k;
cin>>n>>k;
vector<ll> a(n, 0);
ll suma=0;
rep(i, n) cin>>a[i], suma+=a[i];
sort(a.begin(), a.end());
ll l=a[0], r=(suma+n-1)/n + 1;
while(r-l>1){
ll mid=(l+r)/2;
ll req=0, exc=0;
rep(i, n) req+=max<ll>(0, mid-a[i]), exc+=max<ll>(0, a[i]-mid-k);
if(req>exc) r=mid;
else l=mid;
}
ll req=0, exc=0, reqs=0, excs=0, ans=0;
rep(i, n){
if(a[i]<=l) reqs++, req+=l-a[i], a[i]=l;
if(a[i]>=l+k+1) excs++, exc+=a[i]-l-k, a[i]=l+k;
}
exc -= req;
rep(i, min<ll>(excs, exc)) a[n - 1 - i]++;
exc -= excs;
rep(i, exc) a[i]++, req++;
ans += req * k;
for(int i = 0; i < n; ++i) ans += (a[i] * (a[i] + 1)) / 2;
// rep(i, n) cout << a[i] << " ";
// cout << "\n";
cout << ans <<"\n";
}
}
//��
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 1111111;
const LL inf = 1e18;
int n,k,a[N];
LL s[N];
int main(){
int T,i,l,r,h;
LL x,y,z,o,p,t;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
scanf("%d",a+i);
sort(a+1,a+n+1);
for(i=1;i<=n;i++)
s[i]=s[i-1]+a[i];
z=inf,o=-1;
l=0,r=N;
while(l<=r){
h=l+r>>1;
i=lower_bound(a+1,a+n+1,h)-a-1;
x=(LL)i*h-s[i];
i=lower_bound(a+1,a+n+1,h+k)-a-1;
y=(s[n]-s[i])-(LL)(n-i)*(h+k);
if(z>max(x,y))
z=max(x,y),o=h;
if(x<y)
l=h+1;
else
r=h-1;
}
//cout<<z<<' '<<o<<endl;
p=z*k;
t=0;
for(i=1;i<=n;i++){
x=min(max((LL)a[i],o),o+k);
p+=(LL)x*(x+1)/2;
t+=x-a[i];
}
if(t>0)
p-=(o+k)*t;
if(t<0)
p+=(o+1)*-t;
printf("%lld\n",p);
}
return 0;
}
2120F — Superb Graphs
Idea: Dragmon Solution: Dragmon Prepared by: picramide, Dragmon
If two vertices have same open neighborhood in some graph $$$G_i$$$, atleast one of them has to correpond to a clique in every $$$G_i, H_i$$$ pair.
If two vertices have same closed neighborhood in some graph $$$G_i$$$, atleast one of them has to correpond to an independent set in every $$$G_i, H_i$$$ pair.
In a graph $$$G$$$, two vertices $$$v_1$$$ and $$$v_2$$$ are said to be of type1 if they are not adjacent and have the same open neighborhood, i.e., $$$v_1v_2 \not\in E(G)$$$ and $$$N_G(v_1)=N_G(v_2)$$$.
In a graph $$$G$$$, two vertices $$$v_1$$$ and $$$v_2$$$ are said to be of type2 if they are adjacent and have the same closed neighborhood, i.e., $$$v_1v_2 \in E(G)$$$ and $$$N_G[v_1]=N_G[v_2]$$$.
An equivalence class is defined as the set of vertices of the same type. Here, note that if two vertices of type1 are present, then both vertices can't correspond to independent sets as we can merge them to a larger independent set and denote it by a single vertex. Similarly, if two vertices of type2 are present, then both can't correspond to cliques as we can merge them to a larger clique and denote it by a single vertex.
Proof:
- Graph is superb $$$\rightarrow$$$ Every type1 pair has atleast one vertex correspond to a clique and every type2 pair has atleast one vertex correspond to an independent set(We'll prove contrapositive).
Assume there exists a type1 pair $$$v_1v_2$$$ that has both vertices corresponding to an independent set. Let their vertex set be $$$V_1$$$ and $$$V_2$$$. Consider the set $$$V=V_1 \cup V_2$$$ and a new vertex $$$v$$$ that has the same neighborhood as $$$v_1(or v_2)$$$. Each vertex in the set $$$V$$$ satisfies all of the $$$4$$$ conditions given, as if vertices in set $$$V_1$$$ were connected to a vertex, so is every vertex in the set $$$V_2$$$ and vice versa. Hence, we can merge both $$$v_1$$$ and $$$v_2$$$ into a single vertex $$$v$$$ and still satisfy the conditions, making the graph not superb as it doesn't have minimum order. Same reasoning goes for a type2 pair.
- Every type1 pair has atleast one vertex correspond to a clique and every type2 pair has atleast one vertex correspond to an independent set $$$\rightarrow$$$ Graph is superb.
Consider a graph $$$G({v_i}, E)$$$. Let its fun graph be $$$G'({V_i}, E')$$$. Observe that if two vertices $$$V_i$$$ and $$$V_j$$$ have different neighborhoods (excluding each other), then we can't have any fun graph $$$G2'$$$ in which a vertex $$$K$$$ contains some non-zero vertices of $$$V_i$$$ and some non-zero vertices of $$$V_j$$$. This is because if that happens, then it means all vertices in set $$$K$$$ have the same neighborhood, excluding each other, meaning that $$$V_i$$$ and $$$V_j$$$ have the same neighborhood, excluding each other, a contradiction. For the same reasons, if two vertices have the same neighborhood, excluding each other, they have to be either a type1 or type2 pair for there to exist another fun graph $$$G2'$$$ in which a vertex $$$K$$$ contains some non-zero vertices from both sets. Suppose every type1 pair has at least one vertex corresponding to a clique, and every type2 pair has at least one vertex corresponding to an independent set. In that case, there can't be any fun graph $$$G2'$$$ in which a vertex K contains some non-zero vertices from two sets of $$$G'$$$ as set $$$K$$$ has either all vertices in it connected or none of them connected. So, in $$$G2'$$$, each vertex $$$K$$$ corresponds to a subset of vertices of $$$G'$$$, implying that it has at least as many vertices as $$$G'$$$. So, $$$G'$$$ is of minimum cardinality and so is superb. \end{enumerate}
So, for this problem, a vertex is assigned $$$0$$$ if it is assigned an independent set and a vertex is assigned $$$1$$$ if it is assigned a clique. Consider two vertices $$$a$$$ and $$$b$$$. If both are type1, then we can denote it by $$$a \lor b$$$(both can't be I.S.) and if both are type2, we can denote it by $$$\lnot a \lor \lnot b$$$(Both can't be cliques). We can make a $$$2$$$-sat equation using this. If the $$$2$$$-sat has a solution, then the graph is a superb graph. Else, it is not. Expected complexity is $$$O(n^3)$$$
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
bool twoSAT(vector<vector<int>> &adj, vector<vector<int>> &adj_rev, vector<bool> &assignment) {
int n = adj.size();
vector<int> order;
vector<bool> used(n, false);
function<void(int)> dfs1 = [&](int v) {
used[v] = true;
for(auto u : adj[v])
if(!used[u])
dfs1(u);
order.push_back(v);
};
vector<int> comp(n, -1);
function<void(int, int)> dfs2 = [&](int v, int color) {
comp[v] = color;
for(auto u : adj_rev[v])
if(comp[u] == -1)
dfs2(u, color);
};
for(int i = 0; i < n; ++i)
if(!used[i])
dfs1(i);
used.assign(n, false);
for(int i = 0, j = 0; i < n; ++i) {
int v = order[n - i - 1];
if(comp[v] == -1)
dfs2(v, j++);
}
assignment.assign(n / 2, false);
for(int i = 0; i < n; i += 2) {
if(comp[i] == comp[i + 1])
return false;
assignment[i / 2] = comp[i] > comp[i + 1];
}
return true;
}
void solve() {
int n, k; cin >> n >> k;
vector<vector<int>> adj(2*n), adj_rev(2*n);
auto add_or = [&](int a, bool nega, int b, bool negb) {
a = (2*a) + nega;
b = (2*b) + negb;
adj[a^1].push_back(b);
adj[b^1].push_back(a);
adj_rev[b].push_back(a^1);
adj_rev[a].push_back(b^1);
};
function<void(int, vector<vector<int>>&)> process = [&n, &add_or](int m, vector<vector<int>> &adj) {
map<vector<int>, vector<int>> mp;
for(int i = 0; i < n; ++i) {
auto temp = adj[i];
temp.push_back(i);
sort(temp.begin(), temp.end());
mp[temp].push_back(i);
}
for(auto [x, y] : mp) {
for(int i = 0; i < y.size(); ++i) {
for(int j = i + 1; j < y.size(); ++j) {
add_or(y[i], 1, y[j], 1);
}
}
}
mp.clear();
for(int i = 0; i < n; ++i) {
sort(adj[i].begin(), adj[i].end());
mp[adj[i]].push_back(i);
}
for(auto [x, y] : mp) {
for(int i = 0; i < y.size(); ++i) {
for(int j = i + 1; j < y.size(); ++j) {
add_or(y[i], 0, y[j], 0);
}
}
}
};
for(int i = 0; i < k; ++i) {
int m; cin >> m;
vector<vector<int>> adj(n);
for(int j = 0; j < m; ++j) {
int x, y; cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
process(m, adj);
}
vector<bool> assignment(n, false);
cout << ((twoSAT(adj, adj_rev, assignment)) ? "Yes" : "No");
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int _TC = 0; cin >> _TC;
for(int _ct = 1; _ct <= _TC; ++_ct) {
solve(); cout << endl;
}
}
When is a graph a superb graph of itself?
2120G — Eulerian Line Graph
Idea: Dragmon Solution: Dragmon Prepared by: Dragmon
If $$$G$$$ has an Euler cycle, what can we say about $$$L^k(G), k\geq1$$$
If $$$L(G)$$$ has Euler path, how to determine if $$$L^k(G), k\geq2$$$ has an Euler path?
If $$$L(G)$$$ doesn't Euler tour, but $$$L^2(G)$$$ does, what can we say about structure of $$$G$$$? Can there exist another graph $$$H$$$ such that $$$L(H)=G$$$?
Following are the cases where Euler tour is possible:
For the case of Euler cycle-
If $$$G$$$ has an Euler cycle, then $$$L(G)$$$ always has an Euler cycle.
If after removing the two odd-degree vertices of $$$G$$$, the remaining graph is fully disconnected(i.e. the odd-degree vertices form a vertex cover), then $$$L^2(G)$$$ has an Euler cycle.
For the case of Euler path-
If $$$G$$$ doesn't have an Euler tour but $$$L(G)$$$ does, then there is no graph $$$H$$$ such that $$$G=L^2(H)$$$, and any of the graph $$$L^{-k}(H), k\geq0$$$ has an Euler tour. So, we only need to consider cases where $$$G$$$ has an Euler tour, $$$L(G)$$$ doesn't, but $$$L^2(G)$$$ does, or where both $$$G$$$ and $$$L(G)$$$ have Euler tour.
If $$$G$$$ has an Euler path, $$$L(G)$$$ doesn't, but $$$L^2(G)$$$ does, then $$$G$$$ has exactly two odd-degree vertices $$$v_1$$$ and $$$v_2$$$. Additionally, graph obtained after removing $$$v_1$$$ and $$$v_2$$$ from $$$G$$$(i.e. $$$G\backslash$$${$$$v_1,v_2$$$}) consists of exactly one connected component with more than one vertex, and the rest are isolated vertices. Additionally, each connected component/isolated vertex in $$$G\backslash$$${$$$v_1,v_2$$$} has exactly two edges connecting them to $$$v_1$$$, $$$v_2$$$ in $$$G$$$. Here, $$$L^k(G), k\geq3$$$ won't have Euler tour.
If $$$G$$$ has an Euler tour and $$$L(G)$$$ also has an Euler tour, find the smallest trailing path of $$$G$$$(trailing path means a path where the degree of all vertices is either $$$1$$$ or $$$2$$$. There will be only $$$2$$$ of them as $$$G$$$ has an Euler tour) and return its length, and that will be the answer as the length of a trailing path decreases by $$$1$$$ from $$$G$$$ to $$$L(G)$$$.
All of this can be checked in initial graph itself in $$$O(n+m)$$$ time.
//Written By Aryan Sanghi
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i, n) for(ll i = 0; i < n;i++)
#define pb push_back
vector<vector<ll> > v;
bool EulerCycle(ll k)
{
ll odd = 0, n=v.size();
rep(i, n) if(v[i].size() % 2) odd++;
if(odd == 0) return true;// G has all even degree vertices, so it has Euler Cycle
rep(i, n)
rep(j, v[i].size())
if(v[v[i][j]].size()%2==v[i].size()%2){
// cout<<i+1<<" "<<v[i][j]+1<<"\n";
return false;
}
if(k>=2) return true;// L^2(G) has all even degree vertices, so it has Euler Cycle
return false;
}
int LongestTrailingPath(ll n, ll p){
if(v[n].size()>2) return 0;
rep(i, v[n].size()) if(v[n][i]!=p) return 1+LongestTrailingPath(v[n][i], n);
}
void MarkVisited(ll n, vector<ll> &visited, vector<vector<ll> > &adj){
visited[n]=1;
rep(i, adj[n].size()){
if(!visited[adj[n][i]]){
MarkVisited(adj[n][i], visited, adj);
}
}
}
bool EulerPath(ll k){
ll oddv[2], temp=0, n=v.size();
rep(i, n) if(v[i].size()%2) oddv[temp++]=i;
ll l[2]={LongestTrailingPath(oddv[0], -1), LongestTrailingPath(oddv[1], -1)};
if(l[0]>l[1]) swap(l[0], l[1]), swap(oddv[0], oddv[1]);
if(l[0]>=k) return true; // shortest longest trailing path is longer than k
if(l[0]>0) return false; // shortest longest trailing path is non zero and less than k
if(l[1]==1 && v[oddv[0]].size()==3 && v[oddv[1]][0] == oddv[0] && k==1) return true; // odd[0] and odd[1] are connected with one one have degree 1 and other having degree 3
if(find(v[oddv[0]].begin(), v[oddv[0]].end(), oddv[1]) != v[oddv[0]].end()) return false; // odd[0] and odd[1] are adjacent
if(k==1 || k>=3) return false; // Special cases left, and k=1, k>=4 don't satisfy it
vector<vector<ll> > tempv(v);
vector<ll> vis(v.size(), 0), noniso;
rep(i, n){
if(find(tempv[i].begin(), tempv[i].end(), oddv[0]) != tempv[i].end()) tempv[i].erase(find(tempv[i].begin(), tempv[i].end(), oddv[0]));
if(find(tempv[i].begin(), tempv[i].end(), oddv[1]) != tempv[i].end()) tempv[i].erase(find(tempv[i].begin(), tempv[i].end(), oddv[1]));
if(tempv[i].size()>0 && i!=oddv[0] && i!=oddv[1]) noniso.pb(i);
}
MarkVisited(noniso[0], vis, tempv);
rep(i, noniso.size()){
if(!vis[noniso[i]]) return false; // After removing odd degree vertices, there is more than one component
}
ll cntedge=0;
rep(i, noniso.size()){
int temp=0;
if(find(v[noniso[i]].begin(), v[noniso[i]].end(), oddv[0]) != v[noniso[i]].end()) cntedge++, temp++;
if(find(v[noniso[i]].begin(), v[noniso[i]].end(), oddv[1]) != v[noniso[i]].end()) cntedge++, temp++;
if(temp && v[noniso[i]].size() > 2) return false; // Vertex connected to odd vertex has degree exactly 2
}
if(cntedge>2) return false; // More than 2 edges are connected to odd degree vertices from the component
return true; // Special case, k=2 is always satisfied
}
void solve(){
ll n, m, k;
cin>>n>>m>>k;
v.clear();
v.resize(n);
rep(i, m){
ll x, y;
cin>>x>>y;
v[x-1].pb(y-1);
v[y-1].pb(x-1);
}
if(EulerCycle(k)){
cout<<"YES\n";
return;
}
if(EulerPath(k)){
cout<<"YES\n";
return;
}
cout<<"NO\n";
}
int main()
{
ll t;
cin>>t;
while(t--)
{
solve();
}
}
//��
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 222222;
int n,m,k,a[N],b[N],d[N],r[N][2];
vector<int> v[N],c;
LL solve1(){
int i;
LL s=0;
for(i=1;i<=m;i++)
s+=d[a[i]]%2!=d[b[i]]%2;
return s;
}
LL solve2(){
int i;
LL s=0;
for(i=1;i<=n;i++)
s+=(LL)r[i][0]*r[i][1];
return s;
}
LL solve3(){
int i,o;
LL s=0;
for(i=1;i<=n;i++)
s+=(LL)r[i][0]*r[i][1]*(d[i]-2);
for(i=1;i<=m;i++){
o=(d[a[i]]+d[b[i]])%2;
s+=(LL)(r[a[i]][0]-(d[b[i]]%2==0))*(r[b[i]][o^1]-(d[a[i]]%2==(o^1)))+(LL)(r[a[i]][1]-(d[b[i]]%2==1))*(r[b[i]][o]-(d[a[i]]%2==o));
}
return s;
}
int dfs(int u,int fa=0){
if(d[u]>=3)
return 0;
if(d[u]==1)
return N;
int i,x,r=N;
for(i=0;i<v[u].size();i++){
x=v[u][i];
if(x!=fa)
r=min(r,dfs(x,u)+1);
}
return r;
}
int main(){
int T,i,f;
LL s,t;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=m;i++)
scanf("%d%d",a+i,b+i);
for(i=1;i<=m;i++)
d[a[i]]++,d[b[i]]++;
for(i=1;i<=m;i++)
r[a[i]][d[b[i]]%2]++,r[b[i]][d[a[i]]%2]++;
for(i=1;i<=m;i++)
v[a[i]].push_back(b[i]),v[b[i]].push_back(a[i]);
if(k==1)
f=solve1()<=2;
if(k==2)
f=solve2()<=2;
if(k==3)
f=solve3()<=2;
if(k>3){
s=solve2();
if(s==0)
f=1;
if(s>=4)
f=0;
if(s==2){
t=solve3();
if(t==0)
f=1;
if(t>=4)
f=0;
if(t==2){
c.clear();
for(i=1;i<=n;i++)
if(r[i][0]&&r[i][1])
c.push_back(i);
if(c.size()!=2)
f=0;
else
f=k<=min(dfs(c[0]),dfs(c[1]))+1;
}
}
}
if(f)
printf("YES\n");
else
printf("NO\n");
for(i=1;i<=n;i++)
d[i]=0,r[i][0]=0,r[i][1]=0,v[i].clear();
}
return 0;
}
The removed problem was supposed to be G in the Div. 1 + Div. 2, the same problem exists and is available here.
Let $$$s_i$$$ be the maximum number such that array $$$[a_1, a_2, ...a_i-1, s_i, a_i+1, .., a_n]$$$ is valid.
Lemma 1: There exists at most one index $$$i$$$ such that $$$a_i \ge s_i$$$.
Proof 1: Assume there exist two indices $$$i$$$ and $$$j$$$ such that $$$a_i \ge s_i$$$ and $$$a_j \ge s_j$$$, say $$$i \lt j$$$.
For an index $$$i \lt j$$$, $$$a_i \ge s_i$$$ when $$$a_j \ge s_j \Rightarrow$$$
$$$\text{Eq}_1$$$ and $$$\text{Eq}_2 \Rightarrow$$$
since $$$i \lt j \Rightarrow$$$
$$$\text{Contradiction!!}$$$
Lemma 2: If $$$a_i \le s_i$$$ $$$\forall$$$ $$$i \in [1, n]$$$, $$$a$$$ is valid.
Proof 2: Backwards induction — let's divide it into two cases.
Case 1. $$$a_i \lt s_i$$$ $$$\forall$$$ $$$i \in [1, n]$$$. Let $$$j \gt 0$$$ be the smallest index such that $$$a_j \gt 0$$$ and say $$$j$$$-th index car overtakes the one ahead $$$i.e.,$$$
- For all $$$i \lt j-1$$$ and $$$i \gt j$$$, $$$a_i \lt s_i$$$ holds as they were unaffected, why?
- At index $$$j-1$$$ since $$$a_j \lt s_j \Rightarrow a[j]-1 \lt s[j]-1$$$ holds.
- At index $$$j$$$ since $$$a_{j-1} \lt s_{j-1} \Rightarrow a_{j-1} \lt s_{j-1} + a_j$$$ holds.
Case 2. Now, for an index $$$j$$$ if $$$a_j = s_j \Rightarrow$$$ we choose the index $$$k \gt j$$$ such that $$$a_k \gt 0$$$; if there's no such $$$k$$$, we choose $$$j$$$. Condition holds similarly, why?
$$$\text{Solution}$$$
def check(arr, i):
cnt = 0, s = sum(arr[:i - 1]) + i
for j in range(i + 1, n):
if arr[j] <= cnt: cnt += 1
else: s += arr[j] - cnt↵
return arr[i] <= s↵
Let $$$p = [0, a_1, a_1 + a_2, a_1 + a_2 + a_3, .... ]$$$ and say an index $$$i$$$ is critical $$$\Leftrightarrow$$$ $$$i + p[i] \lt a_i$$$.
Lemma 3: It is sufficient to check the greatest critical index $$$i.e.,$$$ if $$$i_1, i_2, ..., i_k$$$ are critical, $$$check(a, i_k)$$$ would be enough to determine whether $$$a$$$ is valid or not.
Proof 3: Trivial, if $$$i_k$$$ performs $$$a_{i_k}$$$ overtakes successfully $$$\Rightarrow$$$ overtakes at indices $$$i \lt i_k$$$ will be nullified by $$$a_{i_k}$$$
Special thanks to Proelectro444 for the formal proof.
$$$\looparrowright$$$ Alternate Solution: $$$O(n \cdot log n)$$$ data structure optimized $$$check(a, i_k)$$$ $$$\forall$$$ $$$k \in [1, n]$$$.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int64_t n, a[N], p[N + 1];
void solve() {
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) p[i] = p[i - 1] + a[i - 1];
int cti = n - 1;
for(int i = n - 1; i >= 0; --i) {
if(i + p[i] < a[i]) {
cti = i;
break;
}
}
int cnt = 0;
int64_t s_i = p[cti] + cti;
for (int j = cti + 1; j < n; ++j) {
if (a[j] <= cnt) ++cnt;
else s_i += a[j] - cnt;
}
if (s_i < a[cti]) cout << "No\n";
else cout << "Yes\n";
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int t; cin >> t;
while(t--) solve();
return 0;
}
#include "bits/stdc++.h"
using namespace std;
using ll = long long int;
mt19937_64 RNG(chrono::high_resolution_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct FT {
vector<ll> s;
FT(int n) : s(n) {}
void update(int pos, ll dif) { // a[pos] += dif↵
for (; pos < size(s); pos |= pos + 1) s[pos] += dif;
}
ll query(int pos) { // sum of values in [0, pos)
ll res = 0;
for (; pos > 0; pos &= pos - 1) res += s[pos-1];
return res;
}
};
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
map<vector<int>, bool> cache;
auto brute = [&] (const auto &self, auto v) -> bool {
if (ranges::max(v) == 0) return true;
if (cache.find(v) != cache.end()) return cache[v];
for (int i = 1; i < v.size(); ++i) {
if (v[i] > 0) {
auto w = v;
--w[i];
swap(w[i], w[i-1]);
bool res = self(self, w);
if (res) return cache[v] = true;
}
}
return cache[v] = false;
};
auto solve = [&] (auto v) {
int n = size(v);
vector<int> b(n);
Tree<array<int, 2>> cur;
for (int i = 0; i < n; ++i) {
if (v[i] == 0) b[i] = i;
else {
// v[i]-th largest element↵
if (cur.size() >= v[i]) {
int want = cur.size() - v[i];
auto [val, _] = *cur.find_by_order(want);
b[i] = val;
}
else b[i] = -1;
}
cur.insert({b[i], i});
}
vector deactivate(n+1, vector<int>());
for (int i = 0; i < n; ++i)
if (b[i] >= 0) deactivate[b[i]].push_back(i);
ll pref = 0;
for (int i = 0; i < n; ++i) pref += v[i] + 1;
FT fen(n);
ll suf = 0;
for (int i = n-1; i >= 0; --i) {
pref -= v[i] + 1;
ranges::reverse(deactivate[i]);
for (int x : deactivate[i]) {
if (v[x]) {
int sub = fen.query(x+1) - fen.query(i+1);
sub = x - i - sub;
suf -= v[x] - sub;
fen.update(x, -1);
}
suf -= fen.query(n) - fen.query(x);
}
ll have = pref + suf;
if (v[i] > have) return false;
if (v[i] > 0) {
suf += v[i];
fen.update(i, 1);
}
}
return true;
};
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector a(n, 0);
for (int &x : a) cin >> x;
if (solve(a)) cout << "Yes\n";
else cout << "No\n";
}
}
If $$$a$$$ is valid, also determine the index of the first overtaker, if multiple are possible, return any one of them.








3000₹ for this
You donot make sense, honestly.
.
exactly
Fast tutorial
There are lots of "Unable to parse markup [type=CF_MATHJAX]", which is giving me a hard time reading the editorial, hope the authors fix this.
why does it error happen though??
It might be because of certain syntax issues, not really sure though!
ok, np, thank u .. I thought my laptop had issues.. glad it is not it.
maybe bug on codeforces. when I read my blog posted 2 months ago, the same errors appear.
yes true, https://mirror.codeforces.com/blog/entry/144048?#comment-1287116
Reloading the site helped me fix it
is it still a rated round??
Yes rates got updated
harshith_04 seeing parsing errors in solutions, please fix them.
so many
Unable to parse markup [type=CF_MATHJAX]I'm getting this in normal firefox, firefox incognito, normal chrome and chrome incognito windows. What to do? 😭
Even then, edge doesn't get a chance ?
I use linux(fedora) so no edge :)
I found a very simple pattern for C during the contest, the answer will be simply the m-n th iteration of bubble sort on 1 2 3....n, but couldnt manage to find a way to get to that in less than O(n^2).
Got tle because of that.
Using binary search and prefix array it can be done
Hey Manas, you can look at my solution. It was a greedy type solution, I made a very silly error at first which gave me -2.
I noticed a pattern for scores $$$n$$$ to $$$2n-1$$$ you can just have 1st element as $$$m - n + 1$$$ and keep rest of the path sorted. For scores $$$\ge 2n-1$$$, you can have first element as $$$n$$$ and recursively call the problem on $$$n-1, m-n$$$. Here is my submission
Ohhh, i too saw that but missed the fact that for 2n-1 and greater numbers I could use recursion. Thankss now I know I was atleast thinking in right direction.
that's actually a pretty smart way to look at the problem
look at mine i used approach that rotation is better like max is sum of n and we can reduce that by rotation i feel mine one is somewhat does what you want in o(n) almost https://mirror.codeforces.com/contest/2120/submission/335760194
So why there are lots of
Unable to parse markup [type=CF_MATHJAX]?me very happy to read the editorial... meanwhile
Unable to parse markup [type=CF_MATHJAX]
This is a CF issue ig, it loads perfectly for me. Try refreshing and maybe open in private window can help.
you are right ... thank you!!!
I tried opening in incognito and I can read the math stuff.. maybe I have installed some extension which is messing up
Yes, the diagrams in problem B were very cute and also very helpful... it saved my time from drawing on my own notebook .. thanks for the effort
G kinda similar to this
https://contest.ucup.ac/contest/1449/problem/7784
Same pic
We were given the aformentioned problem as reference by our coordinator so that I can define line graphs and such. Regarding similarity, they are not similar at all, they just both use concept of line graphs. You can try to solve them both and see for yourself. The pic is same because I took screeonshot of their pic and added it to the problem as it is already available in wikipedia and it is great for illustrating the line graph concept.
No, it's ok
Maybe I should've said "G reminded me of this problem ..."
Regarding the pic it was more like "Hold up, wait a minute" thing
in D,
when calculation permutations of n, doing nCa considers the remaining elements alike but why so?
why not
n! / [ (a — 1)!^(k — 1) * a! ]
there exists a value v which would appear at least 'a' times, now for the tuple, the number of possible values of v are k. Independently, the number of positions at which the value v would appear is a, we can choose any 'a' positions out of a total of n positions(size of row), choosing 'a' distinct objects out of n distinct objects is nCa.
In C problem, isn't it cur += i instead of cur += i + 1?
Why is no one pointing this out!! We add i to check and add i+1 to cur. Also it gives WA. Also why are we looping on j?
Can someone tell me why does Greedy approach doesn't work in Problem E
325545303
I used a MultiSet and find the no of lane changes x between the Longest queue and shortest queue for which the reduction in angriness is maximum. And, I kept doing it until angriness can't be reduced.
I did this one: 325474368
Tried to make it as good as possible, but TLE on pretest 10 repeatedly.
solution for D is quite pretty
"Unable to parse markup [type=CF_MATHJAX]"
What is Mike doing
refreshing the page solved the issue for me
Can you give some more detailed prove about G? I feel like the first two cases of Euler path is only giving conclusions. Maybe my mind is literally not working idk.
And I feel like this whole problem is solved by guessing all the cases for a participant, I(and many ppl imo) didn't pass because not finding out the first two cases of Euler path exists, and finding these two cases is the only difficulty in this problem imo. I thought of the first case might happen at last, but failed to find a case for it, so I went to bed because I'm so sleepy XD
In short I don't think this problem fits a Div1+2 H. I'm glad it got changed to a div2.
The cases were found by doing rigourous case analysis of L^2(G) then L(G) then G. The proof is quite long and convoluted to put in the blog, so I didn't give the proof.
The case analysis revolves by first considering L^2(G) will have 2 odd vertices, means L(G) will have two edges from odd vertices to even vertices and no more, and also L(G) will have atleast 4 odd degree vertices as it doesn't have an Euler tour. Then, there is case analysis on G from cases of L(G), which all boil down to either
After the analysis, you can comfortably arrive at the cases mentioned.
Regarding the problem difficulty, the LGM testers were the only ones to give their feedback, and everyone agreed it is fit for a Div1+2 H. So, that was the reasoning to put it there. But, I do respect your feedback!
Well I seems to understand the method. For me when in contest, I got though the first part of the solution and the third part of Euler path(mostly by seeing the third sample case) pretty fast, which suprised me'caues the position of the problem. maybe it's because I've seen a problem with the same transform method as defination.
imo this problem will have lot more solvers if the sample contains all cases, maybe that's why I feel it's not that hard, but with the sample now it's still hard to figure out the missing cases. Maybe it's just my personal preference, I think cp should be a game of finding solutions, not the cases to solve, maybe it's because i suffered from it a few times in AGC and lose some rating.
I think over the prove for a while and participated in the ARC afterwards, so I write this reply so late. I checked the other problems, I hadn't check tutorial yet, but find it quite challenging, I do appreciate the problems and hardwork from you guys anyway!
Not sure if I'm misunderstanding something — is the claim in Euler path case 1 actually correct? Here, $$$G$$$ doesn't have an Euler tour, $$$L(G)$$$ does, and there is a graph $$$H$$$ such that $$$G=L^2(H)$$$.
Under the context of the problem statement, initial graph G always has an euler tour. Here, L^-2(G) doesn't have an Euler tour, so it can't be the input graph, and there is no L^-3(G) or lesser graphs that we care about. So, we don't mind that case.
I agree with this comment, but the way the editorial is currently phrased is misleading at best (it never says that $$$H$$$ has to have an Euler trail, and even if $$$H$$$ doesn't have an Euler trail, what if $$$L^{-k}(H)$$$ exists and has an Euler trail for some $$$k \gt 0$$$?).
And it seems like it is missing from your case analysis.
I do agree with the misleading part, I'll try to rephrase it better. Regarding case analysis, it did get accounted in it . The only case where $$$G$$$ doesn't have line graph but $$$L(G)$$$ does, only way for $$$G=L^2(H)$$$ is if $$$H$$$ is a long path connected to a $$$K_{1,3}$$$(like your example). But in that case, no further $$$L^{-1}(H)$$$ or beyond is there, so it didn't matter under the context of problem statement.
Thank you so much for the clarification!
Thanks for clarifying.
Is it possible to solve the problem when $$$G$$$ is not constrained to have an Euler trail? In particular, suppose $$$L^k(G)$$$ has an Euler trail for some $$$k \gt 0$$$. If you define $$$k^*$$$ to be the minimum such $$$k$$$, is it true that $$$k^*$$$ is upper bounded by some constant?
If I'm not mistaken, you're claiming $$$k^*\le 3$$$, though is that actually achievable?
That was the original intended problem for the problemset, though it was deemed too much casework and I too didn't have a concrete proof for it, so we went with current version on suggestion by our coordinator, which I believe was the right decision.
Coming back to the problem, if $$$G$$$ is not constrained, then-
The case of Euler trail if $$$G$$$ has a trailing path and Euler cycle remain the same, with just an additional case for Euler cycle when $$$G$$$ is a graph with all odd degree vertices, in which case $$$L(G)$$$ will have an Euler cycle, and modified case where $$$G$$$ is a bipartite graph between odd and even degree vertices as bipartitoons, where $$$L^2(G)$$$ now has Euler cyce.
For the Euler path case, the first statement from the proof remains true, and for all but the case where $$$H$$$ a long path connected to a claw, if $$$G$$$ doesn't have an Euler tour but $$$L(G)$$$ does, there is no graph $$$H$$$ such that $$$G=L^2(H)$$$.
But, the cases for $$$L^{-1}(G)$$$ themselves become more complicated. Now, it isn't simply about removing the two odd degree vertices. The cases will now also involve additional odd degree vertices that were previously ignored, and that form a connected component within themselves or a bipartite graph with Even degree vertices.
Also, now we also need to consider the case where $$$G$$$ themselves doesn't have Euler tour but $$$L(G)$$$ does. I sadly don't remember that part and can't find where I had written it.
So, the claim $$$k^*\leq 3$$$ is true, as it was proven without using the assumption of initial graph not having an Euler tour. Thank you for showing interest into the problem.
Also, while I was thinking more about the problem now, I realized that the case that you told is the only possible case where $$$G$$$ doesn't have an Euler tour, $$$L(G)$$$ does and there is a graph $$$H$$$ such that $$$G=L^2(H)$$$. Attaching any arbitrary long path to a $$$K_{1,3}$$$ doesn't work. And no other case of $$$G$$$ has $$$L^{-1}(G)$$$ as $$$L(H)$$$ for any $$$H$$$ as $$$L^{-1}(G)$$$ always has a claw(a.k.a. $$$K_{1,3}$$$) induced. So it means $$$k^*\leq2$$$ holds.
Oh, does that mean that $$$k^*\le 2$$$ actually holds?
(cuz in the case I shared we have $$$k^*=1$$$)
Yes(sorry for the delay, I have $$$1$$$ response every $$$10$$$ minute penalty for <-100 contribution).
Line graphs are characterized by $$$7$$$ forbidden induced subgraphs, claw a.k.a. $$$K_{1,3}$$$ being one of those, as proven by Beineke and Rousopoullos. But, we only required claw to rule out all possible cases for our problem. That was something that was incredibly interesting to me, because i don't seem to understand why others don't have any contribution to removing the cases and such.
Also, the case you've sent is the case for $$$G$$$ where $$$L^k(G)$$$ has second minimum possible maximum degree over all possible $$$L^k(G)$$$(minimum being when you remove one of the edges of the long path, so basically a claw attached to a single vertex) for a fixed $$$k$$$(This is a proof I have in full version of my paper link, which isn't published yet). So, that's something too.
I'm not sure if I understand the case analysis correctly, but wouldn't
be another valid example where k* = 3?
$$$k^*=2$$$ for the case you've shared, as $$$L^2(H)$$$ has exactly two odd degree vertices.
Ah I see. L^3(H) has an Euler Tour, but I didn't realize L^2(H) also does.
In tutorial of the problem C , it should be loop $$$i$$$ rather than $$$j$$$. Although it does not affect understanding
I don't understand something in C. greedy we constructu $$$ m - n $$$, we have n as remaining sum to construct. now if the tree is like $$$ root = \gt ans[2] = \gt ans[3] = \gt .... = \gt 1 $$$ (ignore the remaining nodes so far), then this will have sum of diviness as $$$ root + ans[2] + ans[3] + .... + 1 $$$ and when adding all other they will have diviness value of 1 so it'll be this $$$ sum + 1.remaining nodes $$$. how is the remaining nodes part made equal to $$$n$$$?
I am a little confused on the editorial of D, it claims there should be (a-1)*k + 1 columns and (b-1) * (k*nca) + 1 rows, but the editorial code prints (a-1)*k + 1 first, then (b-1) * (k*nca) + 1. Doesn't the problem specify the number of rows should be printed first, then the number of columns? If you swap the meaning of column and row in the editorial it makes sense in my opinion or am I misreading something? C and D were interesting problems, shame the contest was ruined by cheaters.
Yeah the editorial have interchanged row and coloumns
The LaTeX of Problem D didn't work well.
$$$D$$$ was a cool one
The
Unable to parse markup [type=CF_MATHJAX]
and Unable to parse markup [type=CF_MATHJAX]
value of Unable to parse markup [type=CF_MATHJAX]
for a divine tree to exist are Unable to parse markup [type=CF_MATHJAX]
and Unable to parse markup [type=CF_MATHJAX]
respectively. Unable to parse markup [type=CF_MATHJAX]
Any Unable to parse markup [type=CF_MATHJAX]
can be achieved similar to exhaustive subset sum with redraws enabled. Let
Unable to parse markup [type=CF_MATHJAX]
, Unable to parse markup [type=CF_MATHJAX]
, Unable to parse markup [type=CF_MATHJAX]
= [ ]. Now, we need to select a multiset of Unable to parse markup [type=CF_MATHJAX]
non-negative integers that sum to Unable to parse markup [type=CF_MATHJAX]
. Unable to parse markup [type=CF_MATHJAX]
Loop Unable to parse markup [type=CF_MATHJAX]
from Unable to parse markup [type=CF_MATHJAX]
Construction: The tree can always be simple path, why?
Tree is rooted at ans[0] . Let's store vis[0:n−1]=false to keep track if a node is visited or not. Loop Unable to parse markup [type=CF_MATHJAX]
from Unable to parse markup [type=CF_MATHJAX]
to Unable to parse markup [type=CF_MATHJAX]
and add edge between Unable to parse markup [type=CF_MATHJAX]
to Unable to parse markup [type=CF_MATHJAX]
and mark all of them Unable to parse markup [type=CF_MATHJAX]
in vis . Take all un-visited in the array Unable to parse markup [type=CF_MATHJAX]
and add an edge from Unable to parse markup [type=CF_MATHJAX]
to Unable to parse markup [type=CF_MATHJAX]
. Loop Unable to parse markup [type=CF_MATHJAX]
from Unable to parse markup [type=CF_MATHJAX]
to Unable to parse markup [type=CF_MATHJAX]
and add an edge from Unable to parse markup [type=CF_MATHJAX]
to Unable to parse markup [type=CF_MATHJAX]
. The divine tree which is a simple path looks like ans[0]↔ . . .
Unable to parse markup [type=CF_MATHJAX]
. . . ↔unvis.back() .
=> "Great" solution :))
In the Solution for Problem E. exc(v+k) > def(v) I think you are missing a equal sign
Alternative approach for E: 325584212
Use a greedy strategy: always move a car from the line with the most cars to the one with the least. While it's easy to compute the cost after each move, doing this directly would take linear time in the total number of cars.
However, the cost function is convex (this can be proven easily, I think), so instead of simulating every move, you can binary search over the possible car-taking-from-lines to find the minimal cost efficiently.
I tried E on Python using a greedy strategy. It sadly gave a TLE ERROR on pretest 10. However, mine was much slower, because I forgot that we can easily find how many cars would we move to the minimum number of cars, and how much to subtract from the initial sum of the cost of the lanes. I think it may be done, but would be highly inefficient for larger lanes. I mean a pure greedy one with the maximum to minimum changes, like the ones I gave changed a bit (include the formulae). Also, I am a Newbie, so, I didn't expect much
Regarding Problem D, it's easy to understand why $$$n = k \times (a-1) + 1$$$ based on the pigeonhole principle.
I was initially confused about the process of deriving the minimum $$$m$$$, but I now roughly understand it. So compared to the solution, I want to provide a more detailed explanation.
Let us consider the problem in this way:
For the first column, suppose we choose color $$$c$$$ to appear exactly $$$a$$$ times, while all other colors appear only $$$a-1$$$ times.
First, I want to explain here why all other colors appear only $$$a-1$$$ times.
Suppose there is another color $$$d$$$ ($$$d \neq c$$$) that also appears $$$a$$$ times. Then, intuitively, having more candidate colors to choose from in the first column makes it easier to later achieve $$$b$$$ columns of the same color (at the very least, it does not make it harder).
Please understand the above statement, as it is crucial for the subsequent derivation.
Now, suppose we are at the $$$j$$$-th column ($$$j \gt 1$$$). Let the colors in rows $$$1$$$ to $$$i$$$ of column $$$j$$$ be $$$f_1, f_2, \dots, f_i$$$. We can deduce three points:
Therefore, we can conclude that the number of configurations per column can be (considered as) $$$k \times \binom{n}{a}$$$.
Thus, the minimum value of $$$m$$$ is finally $$$k \times \binom{n}{a} \times (b-1) + 1$$$.
For colors other than g (which appears exactly a times), the positions of colors appearing a−1 times are unimportant: Since these colors appear only a−1 times in this column, they will never be selected for this column.
This seems somewhat unintutive for me
Can someone see and tell me how am i getting out of bound error 325596815 Like at this point i cant even spot the difference between my code and the editorial code.
In the accumulate you need 0LL
so what do the authors want to express by problem g, actually i gave up immediately once iv found the solution to problem g
Really liked Problem E! D is a cool one too.
D WAS SO SIMPLE ALL ALONG
In the solution of D, I think you've swapped the rows and columns. For example: k*(a-1)+1 isn't the size of each row but the number of rows.
Yes
Yes, thank you for telling. I have rectified it.
Can someone please try to hack my A
O(n) solution for C
325487855
[Question about Problem B] Is it clear from the problem statement what happens when three or more balls collide at the same point at the same time?
[Comment] I personally felt that the overall quality of the problems itself was sufficient.
"It is guaranteed that multiple collisions do not occur at the same moment and position." This is mentioned in the problem statement.
Fun Fact — I discovered this 1 week before the contest that 3-body collisions can be interpreted in multiple ways, so we removed the ambiguity.
Thank you. I overlooked that. I think it would be better if it were also mentioned in the input section.
Can someone explain in D why for choosing column is
"The number of possible values of the tuple is k×nCa " . ?
the size of the tuple is A+1 first element is a number from 1 to K and the next A are positions (from 1 to N) so out of N positions there are NcA choices and K ways to chose the number
note that the positions don't correspond to the chosen first number in the tuple here because we just want to find all the possible tuples.
also N is the number of rows calculated earlier so there are only N choices for the position
cur+= i+1 — maybe cur+=i?
Thank you, yes, it is cur += i. [Updated the Solution]
3000₹ for this
In C Divine Tree why curr += i; not cur+=1?
What was the meaning of rotation in A
Another nice solution for Problem E: Let us consider the operation of moving one car from the longest line to the shortest line. We know that this operation is optimal up to a certain point, until the cost of k exceeds the benefit of the operation. Then we can binary search on the number of operations to find the point where it stops being beneficial. To simulate doing m operations, we can distribute m among the minimum numbers in the array, and distribute -m among the maximum numbers in the array, then track the change to the cost. 326369185
Hello. I have a question about the codes provided for 2120D — Matrix Game.
The case
Your code outputs
16659 1I see you calculate $$$\binom{n}{k}$$$ in $$$O(k)$$$ by multiplying by the modular inverse, but is the correct answer for $$$m$$$ truly 1?
In your counting, the numerator becomes 0, because you multiply it by $$$(n-i+1)$$$, which is $$$0$$$ at some point.
To give another example, let's say you want to calculate $$$\binom{17}{6}$$$ $$$mod$$$ $$$5$$$. The algorithm above will return $$$0$$$ because the numerator becomes $$$0$$$, but the actual answer is $$$12376$$$, which modulo 5 is $$$1$$$
It might be that for the example I shared the answer is 1, but wouldn't there be a case where it fails?
Hey,
As long as the number being multiplied in denominator is not a multiple of $$$mod$$$, the operation of inverse modulo remains valid. For example, $$$\frac{15}{5}\mod 5$$$ is invalid under $$$p\times q^{-1} \mod 5$$$ for $$$\frac{p}{q}$$$ as denominator is multiple of $$$5$$$. The numerator doesn't matter then.
In the problem D, any value in denominator being multiplied is always less than $$$mod$$$ as $$$k \lt mod$$$, so $$$k-i+1 \lt mod$$$ for all $$$1\leq i\leq k$$$. So, it is always coprime to $$$mod$$$, and safe under inverse modulo.
For the first example you've shared, the answer is indeed $$$1$$$ as under modulo, as you can easily prove — if $$$16659$$$ is the value after taking modulo with mod, it means value of $$$n$$$ is $$$mod*r+16659$$$ where $$$r$$$ is some positive integer. So, $$$n-i+1$$$ is divisible by $$$mod$$$ for $$$i=16660 \lt 33335=k$$$. But, the denominator will always be coprime to $$$mod$$$ as shown.
For the second example, it is invalid as some denominator is a multiple of $$$5$$$ that is being multiplied($$$6-i+1$$$ for $$$i=2$$$). So, you first have to reduce numerator and denominator by removing the common factor of $$$5$$$ from both and then find $$$p\times q^{-1}\mod 5$$$
Thanks for the explanation!
E reminded me of this and this
Need Help for C DIVINE TREE submission Link The Log say wrong answer Test 226: Expected -1, got a Tree !! (test case 226) But i have checked the condition of
if you add this condition:
it should fix your code
What does the notation in the solution of D mean? the x^n times C_a? C_a is catalan numbers or something else?
$$$^nC_a$$$ is alternate notation for $$$n \choose a$$$. The authors didn't format it correctly though so it looks like the $$$n$$$ applies to the $$$\times$$$; this tripped me up as well when I read the editorial.
If you want to use the $$$^nC_r$$$ notation in Latex, a simple fix is putting
\,before it, e.g. $$$k\times\,^nC_r$$$ instead of $$$k\times^nC_r$$$.Thank you very much, I was extremely confused why there were Catalan numbers in there haha but that makes much more sense
Thank you! Have rectified in the editorial.
Hello Dragmon! I would like to ask about something in your solution for the problem G, specifically in this line:
Shouldn't this only return true when k = 1? For example, this test case:
1
5 5 6
1 2
2 3
2 4
3 5
5 4
This test case should be valid as it has an Euler trail, and in your code this test case's answer is "YES", while the actual answer should be "NO", I tried it according to my code and logic, any many other codes. Please correct me if I'm wrong, but I had to make sure everything is ok.
Hi. Yes you are right, the answer should be NO. I somehow forgot to add $$$k=1$$$ condition within if and since there weren't any testcase of type where two odd vertices of degree $$$1$$$ and $$$3$$$ are connected with $$$k \gt =2$$$ so my solution passed against the AC benchmark solutions(including brute force one) in polygon.
I see, I got a bit confused while analyzing the logic and code then ended up with a similar conclusion, but your explanation clears it up. it happens so it's alright, and thanks for the reply!
WHY NOT WORKING PLEASE HELP???
For the case
6 3 3 3 3 3
The answer is YES as l1=l2+l3, b2=b3 and l1=b1+b2.
Your code will output NO as it checks b1==b2 and b2==b3 which is true but then in printCheckSquare, it will see 6+3+3!=3.
Thanks bro