QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#338341 | #180. Animal Companion in Maze | chmpro | WA | 56ms | 13376kb | C++20 | 2.6kb | 2024-02-25 20:43:31 | 2024-02-25 20:43:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#ifndef ONLINE_JUDGE
#include "myprettyprint.hpp"
#else
#define dbg(...)
#endif
int now=0;
vector<vector<pair<int,int>>> G;
vector<vector<int>> SCC_GRP;
vector<int> ORD,GRP_NUM,S;
int scc(int x){
ORD[x]=now++;
int rt=ORD[x];
S.push_back(x);
for(auto [y,w]:G[x]) {
if(ORD[y]==-1) rt=min(rt,scc(y));
else if(GRP_NUM[y]==-1) rt=min(rt,ORD[y]);
}
if(rt==ORD[x]){
SCC_GRP.push_back(vector<int>());
while(!S.empty()){
int y=S.back();
SCC_GRP.back().push_back(y);
GRP_NUM[y]=SCC_GRP.size()-1;
S.pop_back();
if(y==x) break;
}
}
return rt;
}
struct length_info{
int mx,mx2,from;
};
vector<length_info> DP;
void dfs_up(int x,int par){
for(auto [y,w]:G[x]) if(y!=par){
dfs_up(y,x);
int m=DP[y].mx;
if(m+1>DP[x].mx){
DP[x].mx2=DP[x].mx;
DP[x].mx=m+1;
DP[x].from=y;
}
else if(m+1>DP[x].mx2) DP[x].mx2=m+1;
}
}
void dfs_down(int x,int par){
for(auto [y,w]:G[x]) if(y!=par){
auto [mx,mx2,from]=DP[x];
if(y==from){
if(mx2+1>DP[y].mx){
DP[y].mx=mx2+1;
DP[y].from=-1;
}
else if(mx2+1>DP[y].mx2) DP[y].mx2=mx2+1;
}
else{
if(mx+1>DP[y].mx){
DP[y].mx=mx+1;
DP[y].from=-1;
}
else if(mx+1>DP[y].mx2) DP[y].mx2=mx+1;
}
}
}
void solve(){
int N,M;cin>>N>>M;
G.resize(N); ORD.resize(N,-1); GRP_NUM.resize(N,-1);
for(int i=0; i<M; i++){
int a,b,w; cin>>a>>b>>w; a--;b--;
if(w==1) G[a].push_back({b,-1});
else {
G[a].push_back({b,i});
G[b].push_back({a,i});
}
}
//1. SCC만들기
for(int i=0; i<N; i++) if(GRP_NUM[i]==-1) scc(i);
dbg(SCC_GRP,GRP_NUM);
//2. Cycle 확인
vector<int> CNT(SCC_GRP.size(),0);
for(int i=0; i<N; i++){
for(auto [j,w]:G[i]) if(GRP_NUM[i]==GRP_NUM[j]){
if(w==-1) {cout<<"Infinite\n"; return;}
if(i<j) CNT[GRP_NUM[i]]++;
}
}
dbg(CNT);
for(int i=0; i<SCC_GRP.size(); i++){
if (CNT[i]>=SCC_GRP[i].size()) {cout<<"Infinite\n"; return;}
}
//3. DP
DP.resize(N);
for(auto& l:DP){l.mx=l.mx2=0; l.from=-1;}
for(int grp=SCC_GRP.size()-1; grp>=0; grp--){
if(SCC_GRP[grp].size()>1){
int x=SCC_GRP[grp][0];
dfs_up(x,-1); dfs_down(x,-1);
}
for(auto x:SCC_GRP[grp]) for(auto [y,w]:G[x]) if(GRP_NUM[x]!=GRP_NUM[y]){
DP[y].mx=max(DP[y].mx,DP[x].mx+1);
}
}
int ans=0;
for(int i=0; i<N; i++) ans=max(ans,DP[i].mx);
cout << ans << '\n';
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3616kb
input:
2 2 1 2 2 1 2 2
output:
Infinite
result:
ok single line: 'Infinite'
Test #2:
score: -100
Wrong Answer
time: 56ms
memory: 13376kb
input:
100000 99999 89699 96762 2 14686 33064 1 59361 58678 1 97303 72987 2 27608 56167 1 2709 58751 2 97188 44066 1 83910 20148 1 98548 95716 1 77379 72035 1 63902 62727 2 13961 28993 2 40194 63930 2 39772 8028 2 67378 91869 2 55853 50932 1 65970 16547 2 86976 77908 1 26350 71286 2 52083 4231 2 73581 3569...
output:
67
result:
wrong answer 1st lines differ - expected: '34', found: '67'