Submission #640548

# Submission time Handle Problem Language Result Execution time Memory
640548 2022-09-14T20:45:22 Z sondos225 Network (BOI15_net) C++17
0 / 100
4 ms 4192 KB
#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;
}

Compilation message

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
1 Correct 2 ms 4180 KB Output is correct
2 Correct 3 ms 4180 KB Output is correct
3 Correct 2 ms 4176 KB Output is correct
4 Correct 2 ms 4180 KB Output is correct
5 Correct 3 ms 4172 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 3 ms 4180 KB Output is correct
8 Correct 2 ms 4180 KB Output is correct
9 Correct 2 ms 4180 KB Output is correct
10 Correct 2 ms 4180 KB Output is correct
11 Correct 3 ms 4140 KB Output is correct
12 Correct 3 ms 4180 KB Output is correct
13 Correct 2 ms 4180 KB Output is correct
14 Correct 2 ms 4180 KB Output is correct
15 Correct 2 ms 4180 KB Output is correct
16 Correct 3 ms 4180 KB Output is correct
17 Correct 4 ms 4180 KB Output is correct
18 Incorrect 3 ms 4192 KB Breaking single line is causing network to disconnect.
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 3 ms 4180 KB Output is correct
3 Correct 2 ms 4176 KB Output is correct
4 Correct 2 ms 4180 KB Output is correct
5 Correct 3 ms 4172 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 3 ms 4180 KB Output is correct
8 Correct 2 ms 4180 KB Output is correct
9 Correct 2 ms 4180 KB Output is correct
10 Correct 2 ms 4180 KB Output is correct
11 Correct 3 ms 4140 KB Output is correct
12 Correct 3 ms 4180 KB Output is correct
13 Correct 2 ms 4180 KB Output is correct
14 Correct 2 ms 4180 KB Output is correct
15 Correct 2 ms 4180 KB Output is correct
16 Correct 3 ms 4180 KB Output is correct
17 Correct 4 ms 4180 KB Output is correct
18 Incorrect 3 ms 4192 KB Breaking single line is causing network to disconnect.
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 3 ms 4180 KB Output is correct
3 Correct 2 ms 4176 KB Output is correct
4 Correct 2 ms 4180 KB Output is correct
5 Correct 3 ms 4172 KB Output is correct
6 Correct 3 ms 4180 KB Output is correct
7 Correct 3 ms 4180 KB Output is correct
8 Correct 2 ms 4180 KB Output is correct
9 Correct 2 ms 4180 KB Output is correct
10 Correct 2 ms 4180 KB Output is correct
11 Correct 3 ms 4140 KB Output is correct
12 Correct 3 ms 4180 KB Output is correct
13 Correct 2 ms 4180 KB Output is correct
14 Correct 2 ms 4180 KB Output is correct
15 Correct 2 ms 4180 KB Output is correct
16 Correct 3 ms 4180 KB Output is correct
17 Correct 4 ms 4180 KB Output is correct
18 Incorrect 3 ms 4192 KB Breaking single line is causing network to disconnect.
19 Halted 0 ms 0 KB -