Codeforces Round 874 G题解

做不动那么多题了,来个G

G就是问你一棵树能切成多少个大小为3的链,想了半天,想过dp啥的,但是后来发现这个贪心就好了,可以证明贪心找不到的,其他方法也找不到

好久没复健了,这是第一次,感觉以后要多做题才可以

#include <bits/stdc++.h>

using namespace std;
constexpr int limit =  (4e5  + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}
int n,m;
int a[limit];
vector<int>g[limit];
int sizes[limit];
int deg[limit];
void solve() {
    cin>>n;
    map<pi(int, int), int>mp;
    rep(i,1,n)g[i].clear(),sizes[i] = 0, deg[i] = 0;
    rep(i,1,n - 1){
        int x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
        mp[{x, y}] = i;
        mp[{y, x}] = i;
        deg[x]++,deg[y]++;
    }
    if(n <= 2){
        cout<<-1<<endl;
        return;
    }
    vector<int>ans;
    int ok = 1;
    function<void(int, int)> dfs = [&](int u, int fa){
        if(!ok)return;
        sizes[u] = 1;
        for(auto && v : g[u]){
            if(v == fa)continue;
            dfs(v, u);
            sizes[u] += sizes[v];
        }
        if(sizes[u] == 3){
            sizes[u] = 0;// clear all cut
            ans.push_back(mp[{fa, u}]);
        }else if(sizes[u] > 3){
            ok = 0;
            return;
        }
    };
    dfs(1, -1);
    if(sizes[1] != 0){
        cout<<-1<<endl;
        return;
    }
    if(ok){
        string oss;
        int num = 0;
        for(auto && it : ans | std::views::filter([](auto const x) -> bool{return x != 0;})){
            oss += to_string(it) + " ";
            ++num;
        }
        oss = to_string(num) + "\n" + oss;
        cout<<oss<<endl;
    }else{
        cout<<-1<<endl;
    }

}
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    int kase;
    cin>>kase;
    while (kase--)
    invoke(solve);
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
    return 0;
}

posted @ 2023-08-12 18:05  tiany7  阅读(8)  评论(0编辑  收藏  举报