Submission #996665

#TimeUsernameProblemLanguageResultExecution timeMemory
996665cpptowinRigged Roads (NOI19_riggedroads)C++17
100 / 100
208 ms82300 KiB
#include<bits/stdc++.h>
#define fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout<<"\n";
#define int long long
#define inf (int)1e18
#define pii pair<int,int>
#define vii vector<pii>
#define lb(x) x&-x
#define bit(i,j) ((i>>j)&1LL)
#define offbit(i,j) (i^(1LL<<j))
#define onbit(i,j) (i|(1LL<<j))
#define vi vector<int>
template <typename T1, typename T2> bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
int n,m;
vii ke[maxn];
pii edge[maxn];
int par[maxn],h[maxn],id[maxn],res[maxn];
int now;
void dfs(int u)
{
    for(auto [v,w] : ke[u]) if(v != par[u])
        {
            id[v] = w;
            par[v] = u;
            h[v] = h[u] + 1;
            dfs(v);
        }
}
struct DSU
{
    vi par;
    DSU(int n) : par(n) {};
    void make(int u)
    {
        par[u] = u;
    }
    int find(int u)
    {
        return u == par[u] ? u : par[u] = find(par[u]);
    }
    void Union(int u,int v)
    {
        u = find(u);
        par[v] = u;
    }
};
main()
{
#define name "TASK"
    if(fopen(name".inp","r"))
    {
        freopen(name".inp","r",stdin);
        freopen(name".out","w",stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    fo(i,1,m) cin >> edge[i].fi >> edge[i].se;
    fo(i,1,n - 1)
    {
        int x;
        cin >> x;
        auto [u,v] = edge[x];
        ke[u].pb(v,x);
        ke[v].pb(u,x);
    }
    dfs(1);
    DSU g(n + 5);
    fo(i,1,n) g.make(i);
    fo(i,1,m)
    {
        if(res[i]) continue;
        auto [u,v] = edge[i];
        u = g.find(u);
        v = g.find(v);
        vi save;
        while(u != v)
        {
            if(h[u] < h[v]) swap(u,v);
            save.pb(id[u]);
            g.Union(par[u],u);
            u = g.find(u);
        }
//        cout << i;en;
//        for(int it : save) cout << it << ' ';en;
        sort(save.begin(),save.end());
        for(auto it : save) res[it] = ++now;
        if(!res[i]) res[i] = ++now;
    }
    fo(i,1,m) cout << res[i] << ' ';
}

Compilation message (stderr)

riggedroads.cpp:71:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   71 | main()
      | ^~~~
riggedroads.cpp: In function 'int main()':
riggedroads.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen(name".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         freopen(name".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...