提出 #67551578


ソースコード 拡げる

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;
using ull = unsigned long long;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
template<class T> using pqmin = priority_queue<T, vector<T>, greater<T>>;
template<class T> using pqmax = priority_queue<T>;
const ll inf=LLONG_MAX/3;
const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define si(x) ll(x.size())
#define rept(n) for(ll _ovo_=0;_ovo_<n;_ovo_++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define rng(i,l,r) for(ll i=l;i<r;i++)
#define gnr(i,l,r) for(ll i=r-1;i>=l;i--)
#define fore(i, a) for(auto &&i : a)
#define fore2(a, b, v) for(auto &&[a, b] : v)
#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)
template<class T> bool chmin(T& a, const T& b){ if(a <= b) return 0; a = b; return 1; }
template<class T> bool chmax(T& a, const T& b){ if(a >= b) return 0; a = b; return 1; }
template<class T, class U> bool chmin(T& a, const U& b){ return chmin(a, (T)b); }
template<class T, class U> bool chmax(T& a, const U& b){ return chmax(a, (T)b); }
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define VLL(name,size) vector<ll>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>> name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>> name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>> name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define SUM(...) accumulate(all(__VA_ARGS__),0LL)
template<class T> auto min(const T& a){ return *min_element(all(a)); }
template<class T> auto max(const T& a){ return *max_element(all(a)); }
template<class T, class F = less<>> void sor(T& a, F b = F{}){ sort(all(a), b); }
template<class T> void uniq(T& a){ sor(a); a.erase(unique(all(a)), end(a)); }
template<class T, class F = less<>> map<T,vector<ll> > ivm(vector<T>& a, F b = F{}){ map<T,vector<ll> > ret; rep(i,si(a))ret[a[i]].push_back(i); return ret;}
template<class T, class F = less<>> map<T,ll> ivc(vector<T>& a, F b = F{}){ map<T,ll> ret; rep(i,si(a))ret[a[i]]++; return ret;}
template<class T, class F = less<>> vector<T> ivp(vector<T> a){ vector<ll> ret(si(a)); rep(i,si(a))ret[a[i]] = i; return ret;}
template<class T, class F = less<>> vector<ll> rev(vector<T> a){ reverse(all(a)); return a;}
template<class T, class F = less<>> vector<ll> sortby(vector<T> a, F b = F{}){vector<ll> w = a; sor(w,b); vector<pll> v; rep(i,si(a))v.eb(a[i],i); sor(v); if(w[0] != v[0].first)reverse(all(v)); vector<ll> ret; rep(i,si(v))ret.pb(v[i].second); return ret;}
template<class T, class P> vector<T> filter(vector<T> a,P f){vector<T> ret;rep(i,si(a)){if(f(a[i]))ret.pb(a[i]);}return ret;}
template<class T, class P> vector<ll> filter_id(vector<T> a,P f){vector<ll> ret;rep(i,si(a)){if(f(a[i]))ret.pb(i);}return ret;}
ll monotone_left(function<bool(ll)> f){ll l = -1,r = (ll)1e18 + 1; assert(f(l + 1) >= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?l:r) = mid;} return l;}
ll monotone_left(ll l,ll r,function<bool(ll)> f){l--; assert(f(l + 1) >= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?l:r) = mid;} return l;}
ll monotone_right(function<bool(ll)> f){ll l = -1,r = (ll)1e18 + 1; assert(f(l + 1) <= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?r:l) = mid;} return r;}
ll monotone_right(ll l,ll r,function<bool(ll)> f){l--; assert(f(l + 1) <= f(r - 1)); while(l + 1 < r){ll mid = (l + r)>> 1; (f(mid)?r:l) = mid;} return r;}
double monotone_double_left(double l,double r,function<bool(double)> f){assert(f(l) >= f(r)); rep(_,100){double mid = (l + r) / 2.0; (f(mid)?l:r) = mid;} return l;}
double monotone_double_right(double l,double r,function<bool(double)> f){assert(f(l) <= f(r)); rep(_,100){double mid = (l + r) / 2.0; (f(mid)?l:r) = mid;} return r;}
template<class S> S unimodal_max(ll l,ll r,function<S(ll)> f){while(l + 2 < r){ll m1 = l + (r - l) / 3,m2 = l + (r - l) / 3 * 2; if(f(m1) < f(m2))l = m1; else r = m2;} S ret = f(l); rng(k,l,r + 1)chmax(ret,f(k)); return ret;}
template<class S> S unimodal_min(ll l,ll r,function<S(ll)> f){while(l + 2 < r){ll m1 = l + (r - l) / 3,m2 = l + (r - l) / 3 * 2; if(f(m1) > f(m2))l = m1; else r = m2;} S ret = f(l); rng(k,l,r + 1)chmin(ret,f(k)); return ret;}
vector<pll> neighbor4(ll x,ll y,ll h,ll w){vector<pll> ret;rep(dr,4){ll xx = x + dx[dr],yy = y + dy[dr]; if(0 <= xx && xx < h && 0 <= yy && yy <w)ret.eb(xx,yy);} return ret;};
vector<pll> neighbor8(ll x,ll y,ll h,ll w){vector<pll> ret;rep(dr,8){ll xx = x + dx[dr],yy = y + dy[dr]; if(0 <= xx && xx < h && 0 <= yy && yy <w)ret.eb(xx,yy);} return ret;};

void outb(bool x){cout<<(x?"Yes":"No")<<"\n";}
ll max(int x, ll y) { return max((ll)x, y); }
ll max(ll x, int y) { return max(x, (ll)y); }
int min(int x, ll y) { return min((ll)x, y); }
int min(ll x, int y) { return min(x, (ll)y); }
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))
ll gcd(ll a,ll b){return (b?gcd(b,a%b):a);}
vector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); per(i,ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
vll prime_table(ll n){vec(ll,isp,n+1,1);vll res;rng(i,2,n+1)if(isp[i]){res.pb(i);for(ll j=i*i;j<=n;j+=i)isp[j]=0;}return res;}
ll powll(lll x,ll y){lll res = 1; while(y){ if(y & 1)res = res * x; x = x * x; y >>= 1;} return res;}
ll powmod(lll x,ll y,lll mod){lll res=1; while(y){ if(y&1)res=res*x%mod; x=x*x%mod; y>>=1;} return res; }
ll modinv(ll a,ll m){ll b=m,u=1,v=0;while(b){ll t=a/b;a-=t*b;swap(a,b);u-=t*v;swap(u,v);}u%=m;if(u<0)u+=m;return u;}

template <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }
template <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }
template <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }
template <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }
template <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }
template <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }
template <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }

template <class T> vector<T> &operator++(vector<T> &v) {
		fore(e, v) e++;
		return v;
}
template <class T> vector<T> operator++(vector<T> &v, int) {
		auto res = v;
		fore(e, v) e++;
		return res;
}
template <class T> vector<T> &operator--(vector<T> &v) {
		fore(e, v) e--;
		return v;
}
template <class T> vector<T> operator--(vector<T> &v, int) {
		auto res = v;
		fore(e, v) e--;
		return res;
}
template <class T> vector<T> &operator+=(vector<T> &l, const vector<T> &r) {
		fore(e, r) l.eb(e);
		return l;
}

template<class... Ts> void in(Ts&... t);
[[maybe_unused]] void print(){}
template<class T, class... Ts> void print(const T& t, const Ts&... ts);
template<class... Ts> void out(const Ts&... ts){ print(ts...); cout << '\n'; }
namespace IO{
#define VOID(a) decltype(void(a))
struct S{ S(){ cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } }S;
template<int I> struct P : P<I-1>{};
template<> struct P<0>{};
template<class T> void i(T& t){ i(t, P<3>{}); }
void i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }
template<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }
template<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }
template<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){ in(get<idx>(t)...); }
template<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ ituple(t, make_index_sequence<tuple_size<T>::value>{}); }
template<class T> void o(const T& t){ o(t, P<4>{}); }
template<size_t N> void o(const char (&t)[N], P<4>){ cout << t; }
template<class T, size_t N> void o(const T (&t)[N], P<3>){ o(t[0]); for(size_t i = 1; i < N; i++){ o(' '); o(t[i]); } }
template<class T> auto o(const T& t, P<2>) -> VOID(cout << t){ cout << t; }
template<class T> auto o(const T& t, P<1>) -> VOID(begin(t)){ bool first = 1; for(auto&& x : t) { if(first) first = 0; else o(' '); o(x); } }
template<class T, size_t... idx> void otuple(const T& t, index_sequence<idx...>){ print(get<idx>(t)...); }
template<class T> auto o(T& t, P<0>) -> VOID(tuple_size<T>{}){ otuple(t, make_index_sequence<tuple_size<T>::value>{}); }
#undef VOID
}
#define unpack(a) (void)initializer_list<int>{(a, 0)...}
template<class... Ts> void in(Ts&... t){ unpack(IO::i(t)); }
template<class T, class... Ts> void print(const T& t, const Ts&... ts){ IO::o(t); unpack(IO::o((cout << ' ', ts))); }
#undef unpack
void solve(){
	ll n,k;
	cin>>n>>k;
	vector<vll> g(n);
	rep(i,n - 1){
		ll u,v;
		cin>>u>>v;
		u--,v--;
		g[u].pb(v);
		g[v].pb(u);
	}
	vll depth(n);
	{
		function<void(ll,ll,ll)> dfs = [&](ll x,ll frm,ll d){
			depth[x] = d;
			fore(y,g[x])if(y != frm){
				dfs(y,x,d + 1);
			}
		};
		dfs(0,-1,0);
	}
	vector<vector<vll> > d(n,vector<vll>(k,vll(2,inf)));
	vector<vector<vll>> ex(n,vector<vll>(k));
	vector<vll> d2(n,vll(k,inf));
	queue<tuple<ll,ll,ll> > que;
	auto push = [&](ll i,ll j,ll b,ll c,ll dist){
		if(b == 0 && si(ex[i][j]) && c != ex[i][j][0] && d2[i][j] > dist){
			d2[i][j] = dist;
			que.push({i,j,b});
			return;
		}
		if(d[i][j][b] <= dist)return;
		d[i][j][b] = dist;
		if(b == 0 && ~c){
			ex[i][j].pb(c);
		}
		que.push({i,j,b});
	};
	push(0,0,1,-1,0);
	while(!que.empty()){
		auto [i,j,b] =que.front();
		// cout<<i<<" "<<j<<" "<<b<<endl;
		que.pop();
		fore(e,g[i]){
			if(depth[i] > depth[e]){
				if(b == 0){
					if(j == k - 1)push(e,0,0,-1,d[i][j][b] + 1);
					else push(e,j + 1,0,i,d[i][j][b]);
				}
			}
			else {
				if(b == 0 && si(ex[i][j]) && e == ex[i][j][0]){
					if(j == k - 1)push(e,0,0,-1,d2[i][j] + 1);
					else push(e,j + 1,1,-1,d2[i][j]);
				}
				else{
					if(j == k - 1)push(e,0,0,-1,d[i][j][b] + 1);
					else push(e,j + 1,1,-1,d[i][j][b]);
				}
			}
		}
	}
	vll ans;
	for(ll i = 1;i < n;i++){
		ans.pb(d[i][0][0] == inf ? -1 : d[i][0][0]);
	}
	out(ans);
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	ll t;
	cin>>t;
	while(t--)solve();
}

提出情報

提出日時
問題 F - Jump Traveling
ユーザ ynymxiaolongbao
言語 C++ 20 (gcc 12.2)
得点 525
コード長 11411 Byte
結果 AC
実行時間 1110 ms
メモリ 481648 KiB

コンパイルエラー

Main.cpp: In function ‘std::vector<long long int> divisor(ull)’:
Main.cpp:76:153: warning: comparison of integer expressions of different signedness: ‘__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type’ {aka ‘long long int’} and ‘ull’ {aka ‘long long unsigned int’} [-Wsign-compare]
   76 | vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); per(i,ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
      |                                                                                                                                 ~~~~~~~~~~~~~~~~~~~~~~~~^~~~
Main.cpp:23:27: note: in definition of macro ‘per’
   23 | #define per(i,n) for(ll i=n-1;i>=0;i--)
      |                           ^

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 525 / 525
結果
AC × 2
AC × 53
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3372 KiB
00_sample_01.txt AC 1 ms 3512 KiB
01_test_00.txt AC 83 ms 3512 KiB
01_test_01.txt AC 159 ms 3744 KiB
01_test_02.txt AC 166 ms 5348 KiB
01_test_03.txt AC 234 ms 20560 KiB
01_test_04.txt AC 388 ms 118036 KiB
01_test_05.txt AC 120 ms 58460 KiB
01_test_06.txt AC 165 ms 78624 KiB
01_test_07.txt AC 630 ms 391740 KiB
01_test_08.txt AC 917 ms 373456 KiB
02_test_00.txt AC 157 ms 3760 KiB
02_test_01.txt AC 165 ms 5456 KiB
02_test_02.txt AC 209 ms 19260 KiB
02_test_03.txt AC 404 ms 136616 KiB
02_test_04.txt AC 115 ms 61948 KiB
02_test_05.txt AC 127 ms 81680 KiB
02_test_06.txt AC 642 ms 391584 KiB
02_test_07.txt AC 922 ms 373868 KiB
03_test_00.txt AC 199 ms 3904 KiB
03_test_01.txt AC 224 ms 5404 KiB
03_test_02.txt AC 282 ms 24392 KiB
03_test_03.txt AC 458 ms 110580 KiB
03_test_04.txt AC 122 ms 66252 KiB
03_test_05.txt AC 173 ms 87104 KiB
03_test_06.txt AC 791 ms 432952 KiB
03_test_07.txt AC 998 ms 450060 KiB
04_test_00.txt AC 967 ms 424012 KiB
04_test_01.txt AC 912 ms 433528 KiB
04_test_02.txt AC 1032 ms 470744 KiB
04_test_03.txt AC 1086 ms 475504 KiB
04_test_04.txt AC 1110 ms 481648 KiB
05_test_00.txt AC 791 ms 374032 KiB
05_test_01.txt AC 692 ms 370132 KiB
05_test_02.txt AC 309 ms 367940 KiB
05_test_03.txt AC 305 ms 368656 KiB
05_test_04.txt AC 283 ms 366812 KiB
05_test_05.txt AC 281 ms 366880 KiB
05_test_06.txt AC 284 ms 368124 KiB
05_test_07.txt AC 285 ms 368980 KiB
06_test_00.txt AC 144 ms 79876 KiB
06_test_01.txt AC 139 ms 80696 KiB
06_test_02.txt AC 144 ms 82172 KiB
07_test_00.txt AC 87 ms 79764 KiB
07_test_01.txt AC 103 ms 79640 KiB
07_test_02.txt AC 113 ms 92260 KiB
07_test_03.txt AC 155 ms 97416 KiB
07_test_04.txt AC 159 ms 92536 KiB
07_test_05.txt AC 328 ms 367484 KiB
07_test_06.txt AC 1049 ms 422024 KiB
07_test_07.txt AC 381 ms 366712 KiB
07_test_08.txt AC 339 ms 389412 KiB
07_test_09.txt AC 723 ms 417524 KiB