Submission #781140

# Submission time Handle Problem Language Result Execution time Memory
781140 2023-07-12T19:09:11 Z YassineBenYounes Village (BOI20_village) C++17
6 / 100
19 ms 8256 KB
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<pair<ll, ll>, null_type, less<pair<ll,ll>>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd  pair<double,double>
#define vdd  vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////

void usaco_problem(){
    freopen("milkvisits.in", "r", stdin);
    freopen("milkvisits.out", "w", stdout);
}

void init(){
    #ifndef ONLINE_JUDGE
 
freopen("input.txt", "r", stdin);
 
freopen("output.txt", "w", stdout);
 
#endif // ONLINE_JUDGE
}

const int mx = 1e3+5;
const int LOG = 25;
const ll inf = 1e18;
const ll mod = 1e8;

vi graph[mx];

bool done[mx];

ll dis[mx][mx];

bool vis[mx];

int ans[mx];

int bfs(int node){
    memset(vis, 0, sizeof vis);
    queue<int> q;
    q.push(node);
    int ans = -1;
    while(!q.empty()){
        int nd = q.front();
        q.pop();
        if(vis[nd] || done[nd])continue;
        vis[nd] = 1;
        ans = nd;
        for(int adj : graph[nd]){
            q.push(adj);
        }
    }
    return ans;
}

ll res = 0, root = 0;

void solve(int node){
    int nxt = bfs(node);
    if(nxt == node){
        res += dis[node][root];
        ans[node] = root;
        return;
    }
    done[node] = 1;
    res += dis[node][nxt];
    ans[node] = nxt;
    solve(nxt);
}

int cur = 0;

void dfs(int node, int p, int c){
    dis[cur][node] = dis[node][cur] = c;
    for(int adj : graph[node]){
        if(adj == p)continue;
        dfs(adj, node, c+1);
    }
}

void run_case(){
    int n;cin >> n;
    for(int i = 0; i < n-1;i++){
        int a, b;cin >> a >> b;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    for(int i = 1; i <= n;i++){
        cur = i;
        dfs(i, i, 0);
    }
    root = bfs(1);
    solve(root);
    cout << 1 << " " << res << endl;
    for(int i = 1; i <= n;i++){
        cout << 1 << " ";
    }
    cout << endl;
    for(int i = 1; i <= n;i++){
        cout << ans[i] << " ";
    }
    cout << endl;
}

int main(){
    speed;
    int t;
    //cin >> t;
    t = 1;
    while(t--){
        run_case();
    }
}

/*
    NEVER GIVE UP!
    DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
    Your Guide when stuck:
    - Continue keyword only after reading the whole input
    - Don't use memset with testcases
    - Check for corner cases(n=1, n=0)
    - Check where you declare n(Be careful of declaring it globally and in main)
*/

Compilation message

Village.cpp: In function 'void usaco_problem()':
Village.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     freopen("milkvisits.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Village.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen("milkvisits.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Village.cpp: In function 'void init()':
Village.cpp:39:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 | freopen("input.txt", "r", stdin);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Village.cpp:41:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 | freopen("output.txt", "w", stdout);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 340 KB Partially correct
2 Partially correct 1 ms 340 KB Partially correct
3 Partially correct 1 ms 344 KB Partially correct
4 Partially correct 1 ms 348 KB Partially correct
5 Partially correct 1 ms 340 KB Partially correct
6 Partially correct 1 ms 340 KB Partially correct
7 Partially correct 1 ms 340 KB Partially correct
8 Partially correct 1 ms 348 KB Partially correct
9 Partially correct 1 ms 340 KB Partially correct
10 Partially correct 1 ms 340 KB Partially correct
11 Partially correct 1 ms 340 KB Partially correct
12 Partially correct 1 ms 340 KB Partially correct
13 Partially correct 1 ms 352 KB Partially correct
14 Partially correct 1 ms 340 KB Partially correct
15 Partially correct 1 ms 340 KB Partially correct
16 Partially correct 1 ms 340 KB Partially correct
17 Partially correct 1 ms 340 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 2 ms 1876 KB Partially correct
2 Partially correct 5 ms 4308 KB Partially correct
3 Partially correct 5 ms 4300 KB Partially correct
4 Incorrect 19 ms 8256 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 340 KB Partially correct
2 Partially correct 1 ms 340 KB Partially correct
3 Partially correct 1 ms 344 KB Partially correct
4 Partially correct 1 ms 348 KB Partially correct
5 Partially correct 1 ms 340 KB Partially correct
6 Partially correct 1 ms 340 KB Partially correct
7 Partially correct 1 ms 340 KB Partially correct
8 Partially correct 1 ms 348 KB Partially correct
9 Partially correct 1 ms 340 KB Partially correct
10 Partially correct 1 ms 340 KB Partially correct
11 Partially correct 1 ms 340 KB Partially correct
12 Partially correct 1 ms 340 KB Partially correct
13 Partially correct 1 ms 352 KB Partially correct
14 Partially correct 1 ms 340 KB Partially correct
15 Partially correct 1 ms 340 KB Partially correct
16 Partially correct 1 ms 340 KB Partially correct
17 Partially correct 1 ms 340 KB Partially correct
18 Partially correct 2 ms 1876 KB Partially correct
19 Partially correct 5 ms 4308 KB Partially correct
20 Partially correct 5 ms 4300 KB Partially correct
21 Incorrect 19 ms 8256 KB Output isn't correct
22 Halted 0 ms 0 KB -