이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;//find_by_order(ind);//order_of_key()
#define int long long
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define pb emplace_back
#define pf emplace_front
#define yes "YES"
#define no "NO"
#define bigg INT_MAX
#define mp make_pair
#define mt make_tuple
#define debug(x) cout<<(#x)<<" = " <<x<<endl;
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define nn '\n'
#define mms(x,y) memset(x,y,sizeof(x))
#define forr(i,j,n) for (int i=j; i<n; i++)
#define forn(i,j,n) for (int i=j; i>n; i--)
#define cinn(x,y) for(int i=0; i<y; i++) cin>>x[i];
#define fi first
#define se second
#define la "LA"
#define pii pair<int,int>
// to use cmd ctrl+shift+c then g++ name.cpp -o name2.exe then name2 then input
//ctrl+shift+p to open search
//shift+alt+a stack comment2
///power function works in log2b
// int pwr(int a, int b)
// {
// int po = 1;
// while ( b )
// {
// if ( b & 1 )
// {
// po = po * a;
// --b;
// }
// a = a*a;
// b = b/2;
// }
// return po;
// }
int len[500001];
bool cmp (int a,int b)
{
return len[a]<len[b];
}
signed main()
{
fast
// #ifndef LOCAL
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// #endif
int n;
cin>>n;
vector <int> a[n+1];
forr(i,0,n-1)
{
int x,y;
cin>>x >>y;
a[x].pb(y);
a[y].pb(x);
}
vector <int> leaf;
bool lef[n+1]={ };
bool vis[n+1]={ };
forr(i,1,n+1)
{
if (a[i].sz==1)
{
lef[i]=1;
leaf.pb(i);
}
}
int cnt=leaf.sz;
cout<<(cnt/2)+(cnt%2)<<nn;
forr(i,0,leaf.sz)
{
mms(len,-1);
queue <int> q;
if (vis[leaf[i]]==1) continue;
q.push(leaf[i]);
len[leaf[i]]=0;
while(!q.empty())
{
int w=q.front();
q.pop();
forr(i,0,a[w].sz)
{
if (cnt==1 && len[a[w][i]]==-1)
{
len[a[w][i]]=len[w]+1;
q.push(a[w][i]);
}
else if (len[a[w][i]]==-1 && vis[a[w][i]]==0)
{
len[a[w][i]]=len[w]+1;
q.push(a[w][i]);
}
}
}
cnt-=2;
int d1=0, mx=0;
forr(i,1,n+1)
{
if (len[i]>mx && lef[i]==1)
{
mx=len[i];
d1=i;
}
}
vis[leaf[i]]=1;
vis[d1]=1;
cout<<leaf[i]<<' '<<d1<<nn;
}
// mms()
// int cnt=(leaf.sz/2)+(leaf.sz%2);
// cout<<cnt<<nn;
// sort(all(leaf),cmp);
// cnt=leaf.sz;
// // cout<<leaf.sz<<endl<<endl;
// int i=0, j=cnt-1;
// while(cnt>0)
// {
// // cout<<yes<<cnt<<endl;
// if (cnt==1)
// {
// cout<<leaf[i]<<' '<<leaf[leaf.sz-1]<<nn;
// }
// else
// {
// cout<<leaf[i]<<' '<<leaf[j]<<nn;
// i++;
// j--;
// }
// cnt-=2;
// }
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
net.cpp: In function 'int main()':
net.cpp:22:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | #define forr(i,j,n) for (int i=j; i<n; i++)
| ^
net.cpp:83:5: note: in expansion of macro 'forr'
83 | forr(i,0,leaf.sz)
| ^~~~
net.cpp:22:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | #define forr(i,j,n) for (int i=j; i<n; i++)
| ^
net.cpp:94:9: note: in expansion of macro 'forr'
94 | forr(i,0,a[w].sz)
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |