QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#394136 | #4231. Rafting Trip | kevinshan# | TL | 0ms | 0kb | C++17 | 5.4kb | 2024-04-20 05:59:10 | 2024-04-20 05:59:10 |
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define f first
#define s second
int xm[4] = {-1, 0, 0, 1};
int ym[4] = {0, -1, 1, 0};
const int MAXN = 505 * 505;
vector<int> adj[MAXN];
vector<int> radj[MAXN];
int inc[MAXN];
int mark[MAXN];
int vis[MAXN];
bool is_river[MAXN];
vector<int> val[MAXN];
map<int, int> res;
int answer;
int dfs(int x, int p) {
vis[x] = 1;
for(int i:val[x]) {
if(res.count(i) == 0) res[i] = 0;
res[i]++;
}
for(int i:radj[x]) {
if(i == p) continue;
dfs(i,x);
}
answer = max(answer, (int)res.size());
for(int i:val[x]) {
res[i]--;
if(res[i] == 0) {
res.erase(i);
}
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
if (fopen("input.in", "r")) {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
answer = 0;
int n, m;
cin>>n>>m;
char symbol[n][m];
for(int i=0; i<n; i++) for(int j=0; j<m; j++) cin>>symbol[i][j];
int grid[n][m];
int CNT = 0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++) {
grid[i][j] = (CNT++);
}
}
vector<char> riv = {'^','<','>','v'};
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
for(int k=0; k<4; k++){
if(symbol[i][j] == riv[k]) {
int nx = i + xm[k];
int ny = j + ym[k];
if(nx >= 0 && nx < n && ny >= 0 && ny < m) {
if(symbol[nx][ny] == riv[0] || symbol[nx][ny] == riv[1] || symbol[nx][ny] == riv[2] || symbol[nx][ny] == riv[3]) {
adj[grid[i][j]].pb(grid[nx][ny]);
radj[grid[nx][ny]].pb(grid[i][j]);
inc[grid[nx][ny]]++;
}
}
}
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(symbol[i][j] == '#'){
for(int k=0; k<4; k++){
int nx = i + xm[k];
int ny = j + ym[k];
if(nx >= 0 && nx < n && ny >= 0 && ny < m) {
val[grid[nx][ny]].pb(grid[i][j]);
}
}
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(symbol[i][j] == riv[0] || symbol[i][j] == riv[1] || symbol[i][j] == riv[2] || symbol[i][j] == riv[3]) {
is_river[grid[i][j]] = 1;
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(symbol[i][j] == riv[0] || symbol[i][j] == riv[1] || symbol[i][j] == riv[2] || symbol[i][j] == riv[3]) {
} else {
mark[grid[i][j]] = 1;
}
}
}
queue<int> q;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(symbol[i][j] == riv[0] || symbol[i][j] == riv[1] || symbol[i][j] == riv[2] || symbol[i][j] == riv[3]) {
if(inc[grid[i][j]] == 0) {
q.push(grid[i][j]);
mark[grid[i][j]] = 1;
}
}
}
}
while(q.size()) {
int c = q.front();
q.pop();
for(int i:adj[c]) {
inc[i]--;
if(inc[i] == 0) {
mark[i] = 1;
q.push(i);
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(symbol[i][j] == riv[0] || symbol[i][j] == riv[1] || symbol[i][j] == riv[2] || symbol[i][j] == riv[3]) {
if(adj[grid[i][j]].size() == 0) mark[grid[i][j]] = 0;
}
}
}
// for(int i=0; i<n; i++){
// for(int j=0; j<m; j++){
// cout<<mark[grid[i][j]]<<" ";
// }
// cout<<"\n";
// }
// cout<<"\n";
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(mark[grid[i][j]] == 0 && !vis[grid[i][j]]) {
// is cyclical component!
vector<int> comp;
if(adj[grid[i][j]].size() == 0) comp.pb(grid[i][j]);
else {
int cur = grid[i][j];
while(1){
comp.pb(cur);
for(int a:adj[cur]) {
if(is_river[a]) {
cur = a;
}
}
if(cur == grid[i][j]) break;
}
}
for(int x:comp) {
// cout<<x<<" ";
for(int v:val[x]) {
if(res.count(v) == 0) res[v] = 1;
res[v]++;
}
vis[x] = 1;
}
// cout<<"\n";
for(int x:comp) {
for(int a:radj[x]) {
if(!vis[a]) {
dfs(a, x);
}
}
}
res.clear();
// cout<<res.size();
}
}
}
cout<<answer;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
5 6 v<<<#v v#v<.> >>v<<< ..v##^ #<<<.^