QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#749550#2562. Fake Plastic Trees 2arvindr9WA 0ms3576kbC++205.7kb2024-11-15 03:42:482024-11-15 03:42:49

Judging History

你现在查看的是最新测评结果

  • [2024-11-15 03:42:49]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3576kb
  • [2024-11-15 03:42:48]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
 
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}

// Add this operator for __int128
ostream& operator << (ostream &os, __int128 x) {
    if (x < 0) {
        os << '-';
        x = -x;
    }
    if (x > 9) os << (x / 10);
    os << (int)(x % 10);
    return os;
}

template<typename T> void debug(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}

typedef int int2;
#define int __int128
#define pi pair<int, int>
#define vi vector<int>
#define vii vector<vector<int>>
#define vpi vector<pi>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define f first
#define s second

long long t;

const int maxn = 1005;
const int maxk = 51;
vector<int> dp[maxn][maxk];

int2 main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    while (t--) {
        int n, k, l, r;
        {
            long long a, b, c, d;
            cin >> a >> b >> c >> d;
            n = a;
            k = b;
            l = c;
            r = d;
        }

        vector<int> a(n+1);
        lep(i,1,n) {
            long long x;
            cin >> x;
            a[i] = x;
        }
        vector<vector<int>> adj(n+1);
        lep(i,1,n-1) {
            long long u,v;
            cin >> u >> v;
            adj[u].pb(v);
            adj[v].pb(u);
        }

        vector<int> sub(n+1);
        vector<int> num_sub(n+1);

        auto dfs1 = [&](auto &self, int u, int p) -> void {
            int val = a[u];
            int num = 1;
            for (int v: adj[u]) {
                if (v != p) {
                    self(self,v,u);
                    val += sub[v];
                    num += num_sub[v];
                }
            }
            sub[u] = val;
            num_sub[u] = num;
            // assert(sub[u] >= 0);
        };

        dfs1(dfs1,1,0);

        auto simplify = [&](vector<int> a) -> vector<int> {
            if (a.empty()) return a;
            sort(a.begin(),a.end());
            a.erase(unique(a.begin(),a.end()),a.end());
            int n = a.size();
            vector<int> res = {a[0]};
            if (a.size() == 1) return res;
            lep(i,2,n-1) {
                if (a[i] <= res.back() + (r-l)) {
                    // don't push earlier element
                    continue;
                } else {
                    res.pb(a[i-1]);
                }
            }
            res.pb(a.back());
            return res;
        };

        auto dfs2 = [&](auto &self, int u, int p) -> void {
            int sz = num_sub[u];
            vector<vector<int>> curdp(min(k+2,sz+1),vector<int>());
            curdp[0].pb(0);

            for (int v: adj[u]) {
                if (v != p) {
                    self(self,v,u);
                    vector<vector<int>> nextdp(min(k+2,sz+1),vector<int>());
                    int szv = min(k,num_sub[v]);
                    lep(k1,0,sz) {
                        lep(k2,0,szv) {
                            for (int x1: curdp[k1]) {
                                for (int x2: dp[v][k2]) {
                                    if (k1 + k2 <= k+1 and x1 + x2 <= r) {
                                        nextdp[k1+k2].pb(x1+x2);
                                    }
                                }
                            }
                        }
                    }
                    lep(i,0,min(k+1,sz)) {
                        nextdp[i] = simplify(nextdp[i]);
                    }
                    swap(curdp, nextdp);
                }
            }


            // add u
            DEBUG(u);

            DEBUG(curdp);
            lep(i,0,min(k+1,sz)) {
                for (int x: curdp[i]) {
                    int next_val = x + a[u];
                    if (next_val <= r) {
                        dp[u][i].pb(next_val);
                    }
                    if (next_val >= l and next_val <= r and i < k + 1) {
                        dp[u][i+1].pb(0);
                    }
                }
                simplify(dp[u][i]);
                DEBUG(i,dp[u][i]);
            }

        };

        dfs2(dfs2,1,0);

        lep(i,1,k+1) {
            if (!dp[1][i].empty() and dp[1][i][0] == 0) {
                cout << 1;
            } else {
                cout << 0;
            }
        }
        cout << "\n";


        lep(i,1,n) {
            lep(j,0,k) {
                dp[i][j].clear();
            }
        }
    }

    
} 

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/

/*
4 3 1 2
1 1 1 1
1 2
2 3
3 4
*/

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3576kb

input:

3
4 3 1 2
1 1 1 1
1 2
2 3
3 4
4 3 1 2
1 1 1 1
1 2
1 3
1 4
4 3 0 0
1 1 1 1
1 2
1 3
1 4

output:

0111
0011
0001

result:

wrong answer 3rd words differ - expected: '0000', found: '0001'