Submission #682593

#TimeUsernameProblemLanguageResultExecution timeMemory
682593AmmarTracks in the Snow (BOI13_tracks)C++17
21.56 / 100
756 ms310580 KiB
#include <bits/stdc++.h> #include<iostream> #include<iomanip> #include<algorithm> #include<vector> #include<utility> #include<set> #include<unordered_set> #include<list> #include<iterator> #include<deque> #include<queue> #include<stack> #include<set> #include<bitset> #include<random> #include<map> #include<unordered_map> #include<stdio.h> #include<complex> #include<math.h> #include<cstring> #include<chrono> #include<string> // #pragma GCC optimize("Ofast") // #pragma GCC optimize("no-stack-protector") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") // #pragma GCC optimize("fast-math") using namespace std; #define pb push_back #define ff first #define ss second #define vi vector <int> #define vvi vector <vi> #define pii pair<int, int> #define ppi pair<pii, int> #define vii vector<pii> #define mii map<int, int> #define mci map<char, int> #define miv map<int, vi> #define mis map<int, set<int>> #define setbits(n) __builtin_popcount(n) #define all(v) (v).begin(), (v).end() #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl #define endl "\n" #define fo(i,n) for(int i=0;i<n;i++) #define in(a,n) for(int i=0;i<n;i++) cin>>a[i]; #define show2(a, b) cout<<a<<' '<<b<<endl #define show3(a, b, c) cout<<a<<' '<<b<<' '<<c<<endl #define show(arr) for (auto i:arr) cout<<i<<' '; #define Endl endl const long long N=1e6+5; const long long mod=1000000007; //998244353; int n,m,sz; vector<string> s; vi dp; int a[4005][4005],g[4005][4005]; int c[]={-1,0,0,1}; int d[]={0,-1,1,0}; bool inrange(int x, int y) { return x>=0 && x<n && y>=0 && y<m; } void bfs(int i, int j) { queue<pii> q; q.push(pii({i,j})); a[i][j]=sz; while(q.size()) { auto p=q.front();q.pop(); int x=p.ff,y=p.ss; for(int k=0;k<4;k++) { int u=x+c[k],v=y+d[k]; if(inrange(u,v) && s[u][v]==s[i][j] && a[u][v]==-1) { q.push({u,v});a[u][v]=sz; } } } sz++; } void bfs2() { queue<int> q; q.push(0);dp[0]=0; while(q.size()) { int i=q.front();q.pop(); // cout<<i<<endl; for(int j=0;j<sz;j++) { if(g[i][j] && dp[j]>1+dp[i]) { dp[j]=1+dp[i];q.push(j); } } } } void cases(){ cin>>n>>m;sz=0; s.clear();s.resize(n);in(s,n); memset(g,0,sizeof(g)); memset(a,-1,sizeof(a)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(s[i][j]!='.' && a[i][j]==-1) bfs(i,j); } } dp.assign(sz,N); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]==-1)continue; int u=i+1,v=j+1; if(inrange(i,v) && a[i][v]!=a[i][j] && a[i][v]!=-1 ) { g[a[i][v]][a[i][j]]=1;g[a[i][j]][a[i][v]]=1; } if(inrange(u,j) && a[i][j]!=a[u][j] && a[u][j]!=-1) { g[a[u][j]][a[i][j]]=1;g[a[i][j]][a[u][j]]=1; } } } // for(auto x:g) // { // show(x);cout<<endl; // } bfs2(); int ans=0; for(auto x:dp)ans=max(ans,x+1); cout<<ans<<endl; } int32_t main(){ std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); int t=1; // cin>>t; for (int i=0; i<t; i++){ //cout<<"Case #"<<i+1<<": "; cases(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...