結果

問題 No.3719 Share the Tree
コンテスト
ユーザー askr58
提出日時 2026-09-18 22:07:29
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 164 ms / 2,000 ms
+ 1µs
コード長 3,976 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,720 ms
コンパイル使用メモリ 340,212 KB
実行使用メモリ 6,400 KB
平均クエリ数 2.00
最終ジャッジ日時 2026-09-18 22:07:39
合計ジャッジ時間 7,858 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:162:40: warning: 'ans' may be used uninitialized [-Wmaybe-uninitialized]
  162 |                         a.push_back(ans+1);
      |                                     ~~~^~
main.cpp:155:29: note: 'ans' was declared here
  155 |                         int ans;
      |                             ^~~

ソースコード

diff #
raw source code

#include <iostream>
#include <bitset>
#include <random>
#include <chrono>
#include <iomanip>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <ranges>
#include <algorithm>
#include <vector>
using namespace std;
using ll=long long;

#include <atcoder/all>
using mint=atcoder::modint998244353;

ostream& operator<<(ostream& os,const mint& x){
	os<<x.val();
	return os;
}
istream& operator>>(istream& is,mint& x){
	int t;
	is>>t;
	x=t;
	return is;
}

template <typename S,typename T>
ostream& operator<<(ostream& os,const pair<S,T>& p);
template <typename S,typename T>
istream& operator>>(istream& is,pair<S,T>& p);
template <typename T,size_t n>
ostream& operator<<(ostream& os,const array<T,n>& arr);
template <typename T,size_t n>
istream& operator>>(istream& is,array<T,n>& arr);
template <typename T>
ostream& operator<<(ostream& os,const vector<T>& vec);
template <typename T>
istream& operator>>(istream& is,vector<T>& vec);

template <typename S,typename T>
ostream& operator<<(ostream& os,const pair<S,T>& p){
	os<<p.first<<" "<<p.second;
	return os;
}
template <typename S,typename T>
istream& operator>>(istream& is,pair<S,T>& p){
	is>>p.first>>p.second;
	return is;
}

template <typename T,size_t n>
ostream& operator<<(ostream& os,const array<T,n>& arr){
	for(int i=0;i<n;i++)os<<arr[i]<<(i+1==n?"":" ");
	return os;
}
template <typename T,size_t n>
istream& operator>>(istream& is,array<T,n>& arr){
	for(int i=0;i<n;i++)is>>arr[i];
	return is;
}
template <typename T>
ostream& operator<<(ostream& os,const vector<T>& vec){
	for(int i=0;i<(int)vec.size();i++)os<<vec[i]<<(i+1==(int)vec.size()?"":" ");
	return os;
}
template <typename T>
istream& operator>>(istream& is,vector<T>& vec){
	for(int i=0;i<(int)vec.size();i++)is>>vec[i];
	return is;
}

template<class... Vecs>
void input_vec(Vecs&... vs) {
    const auto n = get<0>(tie(vs...)).size();

    for (size_t i = 0; i < n; ++i)
        ((cin >> vs[i]), ...);
}

template <class... Vecs>
void output_vec(const Vecs&... vs) {
    const auto n = get<0>(tie(vs...)).size();

    for (size_t i = 0; i < n; ++i) {
        bool first = true;
        (((cout << (exchange(first, false) ? "" : " ") << vs[i])), ...);
        cout << endl;
    }
}

template <typename T>
vector<T> make_unique(vector<T> vec){
	ranges::sort(vec);
	vec.erase(unique(vec.begin(),vec.end()),vec.end());
	return vec;
}

vector<int> Iota(int n,int s=0){
	vector<int> res(n);
	iota(res.begin(),res.end(),s);
	return res;
}

vector<ll> Iotall(int n,ll s=0){
	vector<ll> res(n);
	iota(res.begin(),res.end(),s);
	return res;
}
void YESNO(bool f){
	if(f)cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
}

using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using vvvl=vector<vector<vector<ll>>>; 
using vi=vector<int>;
using vvi=vector<vector<int>>;
using vvvi=vector<vector<vector<int>>>;
int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	string s;
	cin>>s;
	if(s=="Alice"){
		int n;
		cin>>n;
		cout<<n-2<<endl;
		vvi graph(n);
		vi d(n);
		d[0]=1;
		for(int i=1;i<n;i++){
			int u,v;cin>>u>>v;
			u--;v--;
			d[u]++;d[v]++;
			graph[u].push_back(v);
			graph[v].push_back(u);
		}
		vi a;
		vector<bool> erased(n);
		for(int i=0;i<n-2;i++){
			int idx=n+1;
			for(int j=n-1;j>=1;j--){
				if(d[j]==1&&!erased[j]){
					idx=j;
				}
			}
			erased[idx]=true;
			int ans;
			for(int x:graph[idx]){
				if(!erased[x]){
					ans=x;
					d[x]--;
				}
			}
			a.push_back(ans+1);
		}

		cout<<a<<endl;
	}else{
		int n;
		cin>>n;
		cout<<n-2<<endl;
		vi a(n-2);
		cin>>a;
		for(auto& x:a)x--;
		a.push_back(0);
		set<int> s;
		vi cnt(n);
		for(int x:a)cnt[x]++;
		for(int i=0;i<n;i++){
			if(cnt[i]==0)s.insert(i);
		}
		vi u,v;
		for(int i=0;i<n-1;i++){
			int x=*s.begin();
			s.erase(x);
			u.push_back(x);
			v.push_back(a[i]);
			cnt[a[i]]--;
			if(cnt[a[i]]==0)s.insert(a[i]);
		}
		for(int i=0;i<n-1;i++)cout<<u[i]+1<<" "<<v[i]+1<<endl;
	}
}
		

0