Submission #772123

# Submission time Handle Problem Language Result Execution time Memory
772123 2023-07-03T16:33:39 Z nasam Lampice (COCI19_lampice) C++17
56 / 110
1641 ms 38892 KB
#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define FOD(i,b,a) for (int i = (b), _a = (a); i >= _a; i--)
#define FORE(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define CNTBIT(x) __builtin_popcount(x)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define left ___left
#define right ___right
#define pii pair<int, int>
#define DEBUG(n, a) FOR (i, 1, n) cout << a[i] << ' '; cout << endl;1234567890
#define lob lower_bound // >=
#define upb upper_bound // >
#define endl "\n"

using namespace std;

template<class X, class Y> bool maximize(X &x, Y y){ if (x < y){ x = y; return true; } return false; }
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y){ x = y; return true; } return false; }

#define  gogobruhbruh  ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define  file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }

int MOD = 1e9 + 7;

void add(int &a, int b){ if ((a += b) >= MOD) a -= MOD; }
int sub(int a, int b){ if ((a -= b) < 0) a += MOD; return a; }
int muti(int a, int b){ return (1LL * a * b) % MOD; }
int Pow(int x, int y){ int res = 1; for (; y; y >>= 1){ if (y & 1) res = muti(res, x); x = muti(x, x); } return res; }

const int MAX = 5e4 + 7;
const int INF = 1e9 + 7;
const int BASE = 311;

int num, res = 1, mx;
int val[MAX], sz[MAX], pw[MAX];
vector<unordered_set<int>> dp;
vector<int> edge[MAX];
vector<pii> V;
bool del[MAX];

void dfs(int u, int pre){
    sz[u] = 1;
//    if (pre)
//    cout << pre << '-' << char(val[pre]) << ' ' << u << '-' << char(val[u]) << endl;
    for (int v : edge[u])
        if (!del[v] && v != pre){
//            if (u == 25987){
//                cout << v << ' ' << edge[v].size() << endl;
//                return;
//            }
            dfs(v, u),
            sz[u] += sz[v];
        }
}

int centroid(int u, int pre, int n){
    for (int v : edge[u])
        if (!del[v] && v != pre && sz[v] > n)
            return centroid(v, u, n);
    return u;
}

bool dfs2(int u, int pre, int dep, int up, int down, int target){
    if (dep > target)
        return 0;
    add(up, muti(val[u], pw[dep - 1]));
    if (pre)
        down = muti(down, BASE), add(down, val[u]);
    int code = sub(muti(up, pw[target - dep]), down);
    if (dp[target - dep].find(code) != dp[target - dep].end())
        return 1;   
    for (int v : edge[u])
        if (!del[v] && v != pre){
            if (dfs2(v, u, dep + 1, up, down, target))
                return 1;
            if (pre)
                continue;
            for (auto[dep, code] : V)
                dp[dep].insert(code);
            V.clear();
        }
    maximize(mx, dep);
    V.pb({dep - 1, code});
    return 0;
}

bool calc(int u, int target){
    dfs(u, -1);
    u = centroid(u, -1, sz[u] >> 1);
    mx = 0;
    dp[0].insert(muti(val[u], pw[target - 1]));
    if (dfs2(u, 0, 1, 0, 0, target))
        return 1;
    FOR (i, 1, mx)
        dp[i].clear();
    del[u] = 1;
    for (int v : edge[u])
        if (!del[v])
            if (calc(v, target))
                return 1;
    return 0;
}

bool check(int len){
    FOR (i, 1, num)
        del[i] = 0;
    return calc(1, len);
}

void solve(){
    dp.resize(num + 1);
    pw[0] = 1;
    FOR (i, 1, num)
        pw[i] = muti(pw[i - 1], BASE);
    int left = 0, right = num >> 1, res = 1;
    while (left <= right){
        int mid = (left + right) / 2;
        if (check(mid << 1)){
            res = mid << 1;
            left = mid + 1;
        }else
            right = mid - 1;
    }
    left = 0, right = (num - 1) >> 1;
    while (left <= right){
        int mid = (left + right) >> 1;
        if (check(mid << 1 | 1)){
            maximize(res, mid << 1 | 1);
            left = mid + 1;
        }else
            right = mid - 1;
    }
    cout << res;
}

void read(){
    string s;
    cin >> num >> s;
    FOR (i, 1, num)
        val[i] = s[i - 1];
    FOR (i, 2, num){
        static int u, v;
        cin >> u >> v;
        edge[u].eb(v);
        edge[v].eb(u);
    }
}

int main(){
    gogobruhbruh;
    file("main");
    int test = 1;
//    cin >> test;
    while (test--)
        read(),
        solve();
}

Compilation message

lampice.cpp: In function 'int main()':
lampice.cpp:28:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 | #define  file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
      |                                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:158:5: note: in expansion of macro 'file'
  158 |     file("main");
      |     ^~~~
lampice.cpp:28:95: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 | #define  file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
      |                                                                                       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
lampice.cpp:158:5: note: in expansion of macro 'file'
  158 |     file("main");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1492 KB Output is correct
2 Correct 7 ms 1620 KB Output is correct
3 Correct 17 ms 1780 KB Output is correct
4 Correct 19 ms 1812 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Incorrect 1 ms 1504 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 842 ms 13248 KB Output is correct
2 Correct 860 ms 15136 KB Output is correct
3 Correct 552 ms 21776 KB Output is correct
4 Correct 707 ms 21912 KB Output is correct
5 Correct 1099 ms 16664 KB Output is correct
6 Correct 213 ms 38892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1446 ms 10444 KB Output is correct
2 Correct 1641 ms 10036 KB Output is correct
3 Correct 1477 ms 10660 KB Output is correct
4 Correct 1355 ms 11120 KB Output is correct
5 Correct 1064 ms 13708 KB Output is correct
6 Correct 1164 ms 8820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1492 KB Output is correct
2 Correct 7 ms 1620 KB Output is correct
3 Correct 17 ms 1780 KB Output is correct
4 Correct 19 ms 1812 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Incorrect 1 ms 1504 KB Output isn't correct
7 Halted 0 ms 0 KB -