Submission #67587096
Source Code Expand
// uncomment if you need to cheese
//#pragma GCC target ("avx2")
//#pragma GCC optimize ("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<iostream>
#include<vector>
#include <random>
#include <numeric>
#include <algorithm>
#include <map>
#include<queue>
using namespace std;
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\n'
typedef long long ll;
#define mod1 (ll)1000000007
#define mod2 (ll)998244353
#define pll pair<ll,ll>
typedef long double lb;
// Operator overloads
template<typename T1, typename T2> // cin >> pair<T1, T2>
istream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
template<typename T> // cin >> vector<T>
istream& operator>>(istream &istream, vector<T> &v)
{
for (auto &it : v)
cin >> it;
return istream;
}
template<typename T1, typename T2> // cout << pair<T1, T2>
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
template<typename T> // cout << vector<T>
ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }
// Utility functions
template <typename T>
void print(T &&t) { cout << t << "\n"; }
template <typename T, typename... Args>
void print(T &&t, Args &&... args)
{
cout << t << " ";
print(forward<Args>(args)...);
}
vector<ll>dis[1<<18][20]; // dis[i][j][t] will save distance of ith node with jth mod k val and T th node as its parent in adjacency list(if it = sz(adj) then it's no parents)
vector<ll>adj[1<<18];
vector<ll>mp[1<<18]; // mp[i][j] tells us if j'th edge of i points to k then what edge number of k points to i
ll par[1<<18];
void dfs(ll a=0,ll edge=-1){ // edge represnts index of edge of pointer which points to a
int ind =-1;
for(auto j:adj[a]){
++ind;
if(j==par[a]){
mp[par[a]][edge]=ind;
mp[a][ind]=edge;
continue;
}
par[j]=a;
dfs(j,ind);
}
}
inline ll nextnum(ll a,ll k){
a++;
return a==k?0:a;
}
void solve();
int main() {
io;
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ll t=1,n=1;
cin>>t;
while (t--){
solve();
}
return 0;
}
void solve(){
ll n,k;
cin>>n>>k;
for(ll i(0);i<n;++i){
adj[i].clear();
mp[i].clear();
par[i]=-1;
for(ll j(0);j<k;++j){
dis[i][j].clear();
}
}
for(ll i(1);i<n;++i){
ll a,b;
cin>>a>>b;
a--,b--;
adj[a].push_back(b),adj[b].push_back(a);
}
for(ll i(0);i<n;++i){
for(ll j(0);j<k;++j){
dis[i][j]=vector<ll>(adj[i].size()+1,-1);
}
mp[i].resize(adj[i].size());
}
dfs(0);
// bfs
queue<vector<ll>>q;
q.push({0,0,(ll)adj[0].size()});
dis[0][0][adj[0].size()]=0;
while (q.size())
{
auto cur=q.front();
q.pop();
ll node=cur[0],mod=cur[1],pr=cur[2];
// cout<<cur<<"->"<<dis[node][mod][pr]<<endl;
for(ll i(0);i<adj[node].size();++i){
if(i==pr){continue;} // skip parent
ll nex=adj[node][i];
int pp=nextnum(mod,k);
// cout<<nex<<","<<pp<<","<<mp[node][i]<<endl;
if(dis[nex][pp][mp[node][i]]==-1){
dis[nex][pp][mp[node][i]]=dis[node][mod][pr]+1;
q.push({nex,pp,mp[node][i]});
}
}
}
for(int i(1);i<n;++i){
ll ans=-1;
for(int j(0);j<adj[i].size();++j){
if(dis[i][0][j]==-1){continue;}
else if(ans==-1){
ans=dis[i][0][j];
}
else{
ans=min(ans,dis[i][0][j]);
}
}
ans==-1?cout<<ans<<" ":cout<<ans/k<<" ";
}
cout<<endl;
}
// Do not get stuck on a single approach for long, think of multiple ways
Submission Info
| Submission Time |
|
| Task |
F - Jump Traveling |
| User |
Ujjwal9998 |
| Language |
C++ 17 (gcc 12.2) |
| Score |
0 |
| Code Size |
4263 Byte |
| Status |
WA |
| Exec Time |
372 ms |
| Memory |
322764 KiB |
Compile Error
Main.cpp: In function ‘int main()’:
Main.cpp:79:8: warning: unused variable ‘n’ [-Wunused-variable]
79 | ll t=1,n=1;
| ^
Main.cpp: In function ‘void solve()’:
Main.cpp:123:22: warning: comparison of integer expressions of different signedness: ‘ll’ {aka ‘long long int’} and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
123 | for(ll i(0);i<adj[node].size();++i){
| ~^~~~~~~~~~~~~~~~~
Main.cpp:136:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
136 | for(int j(0);j<adj[i].size();++j){
| ~^~~~~~~~~~~~~~
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
0 / 525 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00_sample_00.txt, 00_sample_01.txt |
| All |
00_sample_00.txt, 00_sample_01.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 02_test_00.txt, 02_test_01.txt, 02_test_02.txt, 02_test_03.txt, 02_test_04.txt, 02_test_05.txt, 02_test_06.txt, 02_test_07.txt, 03_test_00.txt, 03_test_01.txt, 03_test_02.txt, 03_test_03.txt, 03_test_04.txt, 03_test_05.txt, 03_test_06.txt, 03_test_07.txt, 04_test_00.txt, 04_test_01.txt, 04_test_02.txt, 04_test_03.txt, 04_test_04.txt, 05_test_00.txt, 05_test_01.txt, 05_test_02.txt, 05_test_03.txt, 05_test_04.txt, 05_test_05.txt, 05_test_06.txt, 05_test_07.txt, 06_test_00.txt, 06_test_01.txt, 06_test_02.txt, 07_test_00.txt, 07_test_01.txt, 07_test_02.txt, 07_test_03.txt, 07_test_04.txt, 07_test_05.txt, 07_test_06.txt, 07_test_07.txt, 07_test_08.txt, 07_test_09.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 00_sample_00.txt |
AC |
74 ms |
138624 KiB |
| 00_sample_01.txt |
AC |
63 ms |
138588 KiB |
| 01_test_00.txt |
WA |
111 ms |
138656 KiB |
| 01_test_01.txt |
WA |
138 ms |
138736 KiB |
| 01_test_02.txt |
WA |
147 ms |
139724 KiB |
| 01_test_03.txt |
WA |
177 ms |
146656 KiB |
| 01_test_04.txt |
WA |
235 ms |
187864 KiB |
| 01_test_05.txt |
AC |
212 ms |
162984 KiB |
| 01_test_06.txt |
AC |
228 ms |
170300 KiB |
| 01_test_07.txt |
WA |
372 ms |
301620 KiB |
| 01_test_08.txt |
WA |
360 ms |
294312 KiB |
| 02_test_00.txt |
WA |
137 ms |
138792 KiB |
| 02_test_01.txt |
WA |
150 ms |
139724 KiB |
| 02_test_02.txt |
WA |
173 ms |
146928 KiB |
| 02_test_03.txt |
WA |
250 ms |
195480 KiB |
| 02_test_04.txt |
AC |
186 ms |
175332 KiB |
| 02_test_05.txt |
AC |
211 ms |
180848 KiB |
| 02_test_06.txt |
WA |
365 ms |
301600 KiB |
| 02_test_07.txt |
WA |
359 ms |
294264 KiB |
| 03_test_00.txt |
WA |
131 ms |
138792 KiB |
| 03_test_01.txt |
WA |
141 ms |
139432 KiB |
| 03_test_02.txt |
WA |
172 ms |
146608 KiB |
| 03_test_03.txt |
WA |
244 ms |
179920 KiB |
| 03_test_04.txt |
AC |
217 ms |
167572 KiB |
| 03_test_05.txt |
AC |
244 ms |
173880 KiB |
| 03_test_06.txt |
WA |
372 ms |
285504 KiB |
| 03_test_07.txt |
WA |
367 ms |
280404 KiB |
| 04_test_00.txt |
WA |
367 ms |
277248 KiB |
| 04_test_01.txt |
WA |
367 ms |
279008 KiB |
| 04_test_02.txt |
WA |
371 ms |
283320 KiB |
| 04_test_03.txt |
WA |
369 ms |
282240 KiB |
| 04_test_04.txt |
WA |
370 ms |
279924 KiB |
| 05_test_00.txt |
WA |
352 ms |
306328 KiB |
| 05_test_01.txt |
WA |
344 ms |
297340 KiB |
| 05_test_02.txt |
AC |
339 ms |
306184 KiB |
| 05_test_03.txt |
AC |
337 ms |
313752 KiB |
| 05_test_04.txt |
AC |
305 ms |
307908 KiB |
| 05_test_05.txt |
AC |
295 ms |
300724 KiB |
| 05_test_06.txt |
AC |
295 ms |
307120 KiB |
| 05_test_07.txt |
AC |
299 ms |
314508 KiB |
| 06_test_00.txt |
AC |
205 ms |
182212 KiB |
| 06_test_01.txt |
AC |
210 ms |
183096 KiB |
| 06_test_02.txt |
AC |
212 ms |
182448 KiB |
| 07_test_00.txt |
AC |
162 ms |
182204 KiB |
| 07_test_01.txt |
AC |
167 ms |
170620 KiB |
| 07_test_02.txt |
WA |
161 ms |
189860 KiB |
| 07_test_03.txt |
WA |
167 ms |
175672 KiB |
| 07_test_04.txt |
WA |
186 ms |
185668 KiB |
| 07_test_05.txt |
WA |
324 ms |
314804 KiB |
| 07_test_06.txt |
WA |
349 ms |
273796 KiB |
| 07_test_07.txt |
WA |
339 ms |
308120 KiB |
| 07_test_08.txt |
WA |
336 ms |
322764 KiB |
| 07_test_09.txt |
WA |
348 ms |
280072 KiB |