[BZOJ 1924][Sdoi2010]所驼门王的宝藏
1924: [Sdoi2010]所驼门王的宝藏
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 1285 Solved: 574
[Submit][Status][Discuss]Description
Input
第一行给出三个正整数 N, R, C。 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室,类型为 Ti。Ti是一个1~3间的整数, 1表示可以传送到第 xi行任意一列的“横天门”,2表示可以传送到任意一行第 yi列的“纵寰门”,3表示可以传送到周围 8格宫室的“自由<x>门”。 保证 1≤xi≤R,1≤yi≤C,所有的传送门位置互不相同。
Output
只有一个正整数,表示你确定的路线所经过不同藏宝宫室的最大数目。
Sample Input
10 7 7
2 2 1
2 4 2
1 7 2
2 7 3
4 2 2
4 4 1
6 7 3
7 7 1
7 5 2
5 2 1Sample Output
9HINT
测试点编号 N R C 1 16 20 20 2 300 1,000 1,000 3 500 100,000 100,000 4 2,500 5,000 5,000 5 50,000 5,000 5,000 6 50,000 1,000,000 1,000,000 7 80,000 1,000,000 1,000,000 8 100,000 1,000,000 1,000,000 9 100,000 1,000,000 1,000,000 10 100,000 1,000,000 1,000,000
Source
题解
按照题意建好有向边, 跑一遍 $Tarjan$ 缩成 $DAG$ 之后直接 $O(n)$ $DP$ 一遍求最大值即可.
至于快速建边, 可以用 $std::set$ 维护同一行/同一列的所有节点编号, 二维 $std::map$ 维护各个节点的位置. 不过如果所有点都在同一行的话可能会$GG$
参考代码
- #include <set>
- #include <map>
- #include <stack>
- #include <vector>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <iostream>
- #include <algorithm>
- const int MAXV=1e5+;
- const int MAXE=5e6;
- struct Edge{
- int from;
- int to;
- Edge* next;
- };
- Edge E[MAXE];
- Edge* head[MAXV];
- Edge* top=E;
- struct Node{
- int x;
- int y;
- int type;
- };
- Node N[MAXV];
- int v;
- int c;
- int r;
- int ans;
- int clk;
- int scc;
- int dp[MAXV];
- int dfn[MAXV];
- int low[MAXV];
- int size[MAXV];
- int belong[MAXV];
- bool inStack[MAXV];
- std::stack<int> s;
- std::map< int, std::set<int> > sx,sy;
- std::map< int, std::map<int,int> > m;
- void Build();
- void DFS(int);
- void Tarjan(int);
- void SweepEdge();
- void Initialize();
- void Insert(int,int);
- int main(){
- Initialize();
- Build();
- for(int i=;i<=v;i++){
- if(dfn[i]==)
- Tarjan(i);
- }
- SweepEdge();
- for(int i=;i<=v;i++){
- if(dp[i]==)
- DFS(i);
- }
- printf("%d\n",ans);
- return ;
- }
- void Initialize(){
- scanf("%d%d%d",&v,&r,&c);
- for(int i=;i<=v;i++){
- scanf("%d%d%d",&N[i].x,&N[i].y,&N[i].type);
- sx[N[i].x].insert(i);
- sy[N[i].y].insert(i);
- (m[N[i].x])[N[i].y]=i;
- }
- }
- void DFS(int root){
- if(dp[root]!=)
- return;
- else{
- dp[root]=size[root];
- int max=;
- for(Edge* i=head[root];i!=NULL;i=i->next){
- DFS(i->to);
- max=std::max(max,dp[i->to]);
- }
- dp[root]+=max;
- ans=std::max(dp[root],ans);
- }
- }
- void SweepEdge(){
- // puts("SE");
- Edge* ed=top;
- top=E;
- memset(head,,sizeof(head));
- for(Edge* i=E;i!=ed;i++){
- if(belong[i->from]!=belong[i->to])
- Insert(belong[i->from],belong[i->to]);
- }
- }
- void Tarjan(int root){
- dfn[root]=low[root]=++clk;
- inStack[root]=true;
- s.push(root);
- for(Edge* i=head[root];i!=NULL;i=i->next){
- if(dfn[i->to]==){
- Tarjan(i->to);
- low[root]=std::min(low[root],low[i->to]);
- }
- else if(inStack[i->to]){
- low[root]=std::min(low[root],dfn[i->to]);
- }
- }
- if(dfn[root]==low[root]){
- scc++;
- int top;
- do{
- top=s.top();
- inStack[top]=false;
- size[scc]++;
- belong[top]=scc;
- s.pop();
- }while(top!=root);
- }
- }
- void Build(){
- for(int k=;k<=v;k++){
- if(N[k].type==){
- std::map< int, std::set<int> >::iterator px=sx.find(N[k].x);
- for(std::set<int>::iterator i=(*px).second.begin();i!=(*px).second.end();++i){
- if(k!=*i)
- Insert(k,*i);
- }
- }
- else if(N[k].type==){
- std::map< int, std::set<int> >::iterator py=sy.find(N[k].y);
- for(std::set<int>::iterator i=(*py).second.begin();i!=(*py).second.end();++i){
- if(k!=*i)
- Insert(k,*i);
- }
- }
- else if(N[k].type==){
- for(int dx=-;dx<=;dx++){
- for(int dy=-;dy<=;dy++){
- if(dx!=||dy!=){
- int tmp=m[N[k].x+dx][N[k].y+dy];
- if(tmp!=&&tmp!=k){
- Insert(k,tmp);
- }
- }
- }
- }
- }
- }
- }
- inline void Insert(int from,int to){
- // printf("Insert %d to %d\n",from,to);
- top->from=from;
- top->to=to;
- top->next=head[from];
- head[from]=top++;
- }
Backup
[BZOJ 1924][Sdoi2010]所驼门王的宝藏的更多相关文章
- BZOJ 1924: [Sdoi2010]所驼门王的宝藏 【tarjan】
Description 在宽广的非洲荒漠中,生活着一群勤劳勇敢的羊驼家族.被族人恭称为“先 知”的Alpaca L. Sotomon 是这个家族的领袖,外人也称其为“所驼门王”.所 驼门王毕生致力于维 ...
- bzoj 1924 [Sdoi2010]所驼门王的宝藏(构图,SCC,DP)
Description Input 第一行给出三个正整数 N, R, C. 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室 ...
- 【刷题】BZOJ 1924 [Sdoi2010]所驼门王的宝藏
Description Input 第一行给出三个正整数 N, R, C. 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室 ...
- 【题解】SDOI2010所驼门王的宝藏(强连通分量+优化建图)
[题解]SDOI2010所驼门王的宝藏(强连通分量+优化建图) 最开始我想写线段树优化建图的说,数据结构学傻了233 虽然矩阵很大,但是没什么用,真正有用的是那些关键点 考虑关键点的类型: 横走型 竖 ...
- [SDOI2010]所驼门王的宝藏
题目描述 在宽广的非洲荒漠中,生活着一群勤劳勇敢的羊驼家族.被族人恭称为"先知"的Alpaca L. Sotomon是这个家族的领袖,外人也称其为"所驼门王". ...
- [LuoguP2403][SDOI2010]所驼门王的宝藏
题目描述 在宽广的非洲荒漠中,生活着一群勤劳勇敢的羊驼家族.被族人恭称为"先知"的Alpaca L. Sotomon是这个家族的领袖,外人也称其为"所驼门王". ...
- 洛谷 2403 [SDOI2010] 所驼门王的宝藏
题目描述 在宽广的非洲荒漠中,生活着一群勤劳勇敢的羊驼家族.被族人恭称为“先知”的Alpaca L. Sotomon是这个家族的领袖,外人也称其为“所驼门王”.所驼门王毕生致力于维护家族的安定与和谐, ...
- Luogu 2403 [SDOI2010]所驼门王的宝藏
BZOJ 1924 内存要算准,我MLE了两次. 建立$n + r + c$个点,对于一个点$i$的坐标为$(x, y)$,连边$(n + x, i)$和$(n + r + y, i)$,代表这一列和 ...
- BZOJ 1924 && Luogu P2403 [SDOI2010]所驼门王的宝藏 恶心建图+缩点DP
记住:map一定要这么用: if(mp[x[i]+dx[j]].find(y[i]+dy[j])!=mp[x[i]+dx[j]].end()) add(i,mp[x[i]+dx[j]][y[i]+dy ...
随机推荐
- Oracle 存储过程A
create or replace procedure users_procedure is cursor users_cursor is select * from users; v_id user ...
- redis数据类型(六)Sorted set类型
一.sorted set类型 sorted set是有序集合,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,会自动重新按新的值调整顺序.可以理解了有两列的 m ...
- vue之生命周期的一点总结
vue的生命周期的过程提供了我们执行自定义逻辑的机会,好好理解它的生命周期,对我们很有帮助. 一.vue实例的生命周期(vue2.0) 二.生命周期描述:(参考截图) 三.例子 window.vm = ...
- 把AspDotNetCoreMvc程序运行在Docker上-part2:修改容器以及发布镜像
在上一个part<把AspDotNetCoreMvc程序运行在Docker上-part1>,已经将成功将aspdotnetcore程序运行在两个不同的容器中,目前两个容器的内容完全相同,只 ...
- linux ssh 免密码登录的配置过程
# ssh-keygen -t rsa -C "自定义描述" -f ~/.ssh/自定义生成的rsa文件 # cd ./.ssh # touch config # 粘贴 Host ...
- js常用字符处理方法
JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...
- jquery 获取 tagName(JQuery如何得到tagName?)
在javascript中要取得tagName十分简单,但在jQuery中官方文档却没有记载,在一通百度和谷歌之后,尝试了不少所谓秘技,都不能正确得到,经过自己的验证,终于找到了方法,于是记录下来以备忘 ...
- 【9】log4net 实例
一.创建项目并添加nuget: Install-Package log4net 二.添加配置文件 <configuration> <configSections> < ...
- java:模拟队列操作
import java.util.LinkedList; public class Myqueue { private LinkedList<Object> linkedList; pub ...
- Spring入门(一)— IOC、DI
一.Spring介绍 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成 ...