답안 #772114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
772114 2023-07-03T16:26:58 Z nasam Lampice (COCI19_lampice) C++17
56 / 110
1594 ms 39224 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];
unordered_map<int, bool> dp[MAX];
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][code] = 1;
            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][muti(val[u], pw[target - 1])] = 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(){
    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:157:5: note: in expansion of macro 'file'
  157 |     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:157:5: note: in expansion of macro 'file'
  157 |     file("main");
      |     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4256 KB Output is correct
2 Correct 6 ms 4308 KB Output is correct
3 Correct 18 ms 4392 KB Output is correct
4 Correct 20 ms 4444 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Incorrect 3 ms 4308 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 842 ms 13968 KB Output is correct
2 Correct 809 ms 15748 KB Output is correct
3 Correct 526 ms 22276 KB Output is correct
4 Correct 687 ms 22408 KB Output is correct
5 Correct 1088 ms 17020 KB Output is correct
6 Correct 210 ms 39224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1417 ms 10796 KB Output is correct
2 Correct 1594 ms 10380 KB Output is correct
3 Correct 1461 ms 11120 KB Output is correct
4 Correct 1349 ms 11372 KB Output is correct
5 Correct 1103 ms 14452 KB Output is correct
6 Correct 1116 ms 9548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4256 KB Output is correct
2 Correct 6 ms 4308 KB Output is correct
3 Correct 18 ms 4392 KB Output is correct
4 Correct 20 ms 4444 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Incorrect 3 ms 4308 KB Output isn't correct
7 Halted 0 ms 0 KB -