제출 #1090126

#제출 시각아이디문제언어결과실행 시간메모리
1090126andrei_iorgulescuRailway (BOI17_railway)C++14
100 / 100
179 ms49300 KiB
#include <bits/stdc++.h>

using namespace std;

int n,m,k;
vector<int> g[100005], f[100005];
int t[100005];
map<pair<int,int>, int> idx;

int tmp[100005], cur, ult[100005];
int h[100005];
int bl[20][100005], meh[20][100005];

void dfs(int nod)
{
    tmp[nod] = ++cur;
    for (auto vecin : g[nod])
    {
        if (tmp[vecin] == 0)
        {
            t[vecin] = nod;
            h[vecin] = 1 + h[nod];
            f[nod].push_back(vecin);
            dfs(vecin);
        }
    }
    ult[nod] = cur;
}

void adg(int x, int y)
{
    //cout << x << ' ' << y << " yeee " << endl;
    int dh = h[y] - h[x];
    for (int pas = 16; pas >= 0; pas--)
    {
        if (dh >= (1 << pas))
        {
            meh[pas][y]++;
            dh -= (1 << pas);
            y = bl[pas][y];
        }
    }
}

int lca(int x, int y)
{
    if (h[x] < h[y])
        swap(x, y);
    for (int pas = 16; pas >= 0; pas--)
        if (h[x] - (1 << pas) >= h[y])
            x = bl[pas][x];
    if (x == y)
        return x;
    for (int pas = 16; pas >= 0; pas--)
        if (bl[pas][x] != bl[pas][y])
            x = bl[pas][x], y = bl[pas][y];
    return t[x];
}

bool cmp(int A, int B)
{
    return tmp[A] < tmp[B];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n >> m >> k;
    for (int i = 1; i < n; i++)
    {
        int x,y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
        idx[{x,y}] = idx[{y,x}] = i;
    }
    dfs(1);
    for (int i = 1; i <= n; i++)
        bl[0][i] = t[i];
    for (int i = 1; i <= 16; i++)
        for (int j = 1; j <= n; j++)
            bl[i][j] = bl[i - 1][bl[i - 1][j]];
    for (int i = 1; i <= m; i++)
    {
        int cnt;
        cin >> cnt;
        vector<int> uf;
        for (int j = 0; j < cnt; j++)
        {
            int x;
            cin >> x;
            uf.push_back(x);
        }
        sort(uf.begin(), uf.end(), cmp);
        for (int j = 0; j < cnt - 1; j++)
            uf.push_back(lca(uf[j], uf[j + 1]));
        set<int> bruh;
        for (auto it : uf)
            bruh.insert(it);
        uf.clear();
        for (auto it : bruh)
            uf.push_back(it);
        sort(uf.begin(), uf.end(), cmp);
        stack<int> stk;
        stk.push(uf[0]);
        for (int j = 1; j < uf.size(); j++)
        {
            int lol = uf[j];
            while (ult[stk.top()] < tmp[lol])
                stk.pop();
            adg(stk.top(), lol);
            stk.push(lol);
        }
    }
    vector<int> ans;
    for (int pas = 16; pas >= 0; pas--)
    {
        for (int i = 2; i <= n; i++)
        {
            if (pas != 0)
            {
                meh[pas - 1][i] += meh[pas][i];
                meh[pas - 1][bl[pas - 1][i]] += meh[pas][i];
            }
            else
            {
                if (meh[pas][i] >= k)
                    ans.push_back(idx[{i, t[i]}]);
            }
        }
    }
    sort(ans.begin(), ans.end());
    cout << ans.size() << '\n';
    for (auto it : ans)
        cout << it << ' ';
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

railway.cpp: In function 'int main()':
railway.cpp:108:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |         for (int j = 1; j < uf.size(); j++)
      |                         ~~^~~~~~~~~~~
#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...