QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#98991 | #4231. Rafting Trip | Zomara# | WA | 20ms | 53408kb | C++20 | 4.0kb | 2023-04-21 07:54:04 | 2023-04-21 07:54:08 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define el '\n'
#define FIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> pt;
typedef unsigned long long ull;
template<typename T, typename X>
using hashTable = gp_hash_table<T, X>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// mt19937_64 for long long
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
const int N = 3e5 + 5;
int n, m;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int getId(int i, int j)
{
return m * i + j;
}
int inDeg[N];
char grid[502][502];
vector<int> shbak[N];
vector<pair<int, int>> G[N];
set<int> G2[N], shbak2[N];
bool valid(int i, int j)
{
return (i >= 0 && j >= 0 && i < n && j < m);
}
int id;
stack<int> st;
int sz[N], tag[N];
bool vis[N], inStack[N];
vector<int> comp[N], head;
int tin[N], lowLink[N], state[N];
void tarjan(int u)
{
st.push(u);
state[u] = 1;
tin[u] = lowLink[u] = ++id;
for(auto &[i, j]: G[u])
{
int v = getId(i, j);
if(!state[v])
{
tarjan(v);
lowLink[u] = min(lowLink[u], lowLink[v]);
}
else if(state[v] == 1)
lowLink[u] = min(lowLink[u], tin[v]);
}
if(tin[u] == lowLink[u])
{
head.push_back(u);
auto &c = comp[u];
while(st.top() != u)
{
state[st.top()] = 2, tag[st.top()] = u;
c.push_back(st.top());
st.pop();
}
tag[u] = u;
c.push_back(u);
state[u] = 2;
st.pop();
}
}
int ans = 0;
map<int, int> mp;
void dfs(int node)
{
for(auto &it: shbak2[node])
mp[it]++;
ans = max(ans, (int) mp.size());
for(auto &ch: G2[node])
dfs(ch);
for(auto &it: shbak2[node])
{
mp[it]--;
if(mp[it] == 0)
mp.erase(it);
}
}
void doWork()
{
cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> grid[i][j];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(grid[i][j] == '.' || grid[i][j] == '#')
continue;
int ni = i, nj = j;
if(grid[i][j] == '>')
nj++;
else if(grid[i][j] == '<')
nj--;
else if(grid[i][j] == '^')
ni--;
else
ni++;
if(valid(ni, nj) && grid[ni][nj] != '.' && grid[ni][nj] != '#')
G[getId(ni, nj)].emplace_back(i, j);
for(int k = 0; k < 4; k++)
{
ni = i + dx[k], nj = j + dy[k];
if(valid(ni, nj) && grid[ni][nj] == '#')
shbak[getId(i, j)].push_back(getId(ni, nj));
}
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(grid[i][j] == '.' || grid[i][j] == '#')
continue;
tarjan(getId(i, j));
}
}
for(int i = 0; i < n * m; i++)
for(auto &j: G[i])
G2[tag[i]].insert(tag[getId(j.first, j.second)]), inDeg[tag[getId(j.first, j.second)]]++;
for(int i = 0; i < n * m; i++)
for(auto &it: shbak[i])
shbak2[tag[i]].insert(it);
for(int i = 0; i < n * m; i++)
{
if(inDeg[i] == 0 && G2[i].size())
dfs(i);
}
cout << ans;
}
signed main()
{
FIO
int T = 1;
// cin >> T;
for(int i = 1; i <= T; i++)
doWork();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 20ms
memory: 53408kb
input:
5 6 v<<<#v v#v<.> >>v<<< ..v##^ #<<<.^
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 10ms
memory: 52628kb
input:
4 5 >v<<. ^<..# #...# .#>^#
output:
2
result:
ok single line: '2'
Test #3:
score: -100
Wrong Answer
time: 17ms
memory: 52984kb
input:
4 6 >>v#>v ^#>>^v ^<<#v< .#^<<#
output:
0
result:
wrong answer 1st lines differ - expected: '5', found: '0'