이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod =1e9 + 7, pr = 31;
const int mxn = 2500, mxq = 1e5 + 5, sq = 500, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
int n, m, id = 0;
int trie[mxn + 1][2];
bool safe[mxn + 1];
int dis[2505][2505];
vt<pii>events[51];
void solve(){
cin >> n >> m;
int ans = inf;
for(int i = 1; i <= n; i++){
string s; cin >> s;
int nd = 0;
for(int j = 0; j < sz(s); j++){
int c = s[j] - '0';
if(!trie[nd][c])trie[nd][c] = ++id;
nd = trie[nd][c];
}
if(safe[nd])ans = min(ans, sz(s));
safe[nd] = 1;
events[sz(s)].pb(mpp(0, nd));
}
//modified bfs to handle different starting weights
for(int i = 0; i <= id; i++){
for(int j = 0; j <= id; j++){
dis[i][j] = inf;
}
}
queue<pii>q;
for(int i = 1; i <= m; i++){
for(auto j: events[i]){
if(dis[j.fi][j.se] == inf){
dis[j.fi][j.se] = i;
q.push(j);
}
}
while(sz(q)){
auto [p1, p2] = q.front();
if(dis[p1][p2] > i)break;
q.pop();
for(int bit = 0; bit < 2; bit++){
if(trie[p1][bit] && trie[p2][bit]){
int n1 = trie[p1][bit], n2 = trie[p2][bit];
if(dis[n1][n2] == inf){
dis[n1][n2] = dis[p1][p2] + 1;
q.push({n1, n2});
}if(safe[n1]){
//
if(dis[0][n2] == inf){
dis[0][n2] = dis[p1][p2] + 1;
q.push({0, n2});
}
}if(safe[n2]){
if(dis[n1][0] == inf){
dis[n1][0] = dis[p1][p2] + 1;
q.push({n1, 0});
}
}
}
}
}
}
while(sz(q)){
auto [p1, p2] = q.front();
q.pop();
for(int bit = 0; bit < 2; bit++){
if(trie[p1][bit] && trie[p2][bit]){
int n1 = trie[p1][bit], n2 = trie[p2][bit];
if(dis[n1][n2] == inf){
dis[n1][n2] = dis[p1][p2] + 1;
q.push({n1, n2});
}if(safe[n1]){
//
if(dis[0][n2] == inf){
dis[0][n2] = dis[p1][p2] + 1;
q.push({0, n2});
}
}if(safe[n2]){
if(dis[n1][0] == inf){
dis[n1][0] = dis[p1][p2] + 1;
q.push({n1, 0});
}
}
}
}
}
for(int i = 0; i <= id; i++){
for(int j = 0; j <= id; j++){
if(safe[i] && safe[j])ans = min(ans, dis[i][j]);
}
}
cout << ((ans == inf) ? -1: ans) << "\n";
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("THREE.inp", "r", stdin);
//freopen("THREE.out", "w", stdout);
int tt; tt = 1;
while(tt--){
solve();
}
return(0);
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void solve()':
Main.cpp:64:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
64 | auto [p1, p2] = q.front();
| ^
Main.cpp:90:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
90 | auto [p1, p2] = q.front();
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |