K Segment Routing

Segment Routing是一种基于IPv6的下一代IP承载协议,通过在IP包头中添加包含一系列指令的段列表,来替代传统IP路由中逐跳查找路由表的方式。这种方式减少了查找延迟,提高了网络效率。题目描述了一个具体的Segment Routing网络拓扑,给出了一组网络日志记录,要求根据日志中的转发指令恢复目的地地址。解题思路是通过维护每个路由器的出边信息,根据日志中的路径指示进行遍历,找出合法的目标节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Segment Routing over IPv6 is a next-generation IP bearer protocol.
In traditional IP routing, an IP packet sent from a source host goes through multiple routers and then reaches the destination host. Each router checks the destination IP address inside the IP packet header. Then sent the packet to the next router which is closer to the destination. Technically, we call the next closest router “next hop”. Of course, the “next hop” has to be a neighbor of the current router in the graph.
Routers typically maintain a routing table, lists the next hop to particular network destinations. Whenever a new IP packet comes, a router examines the packet header’s destination IP address and compares it against a routing table to determine the packet’s best next hop.
No wonder looking up routing tables takes time, if an IP packet goes through K routers to reach the destination, the looking up process will happen K times at each router. That’s one reason why there is high latency between two distant hosts.
In a segment routed network, an IP packet may be prepended a header that contains a list of “segments”, which are instructions that are executed on subsequent nodes in the network. These instructions may be forwarding instructions, such as an instruction to forward a packet to a specific destination or interface.
We can consider the internet as a directed graph.
For example, consider the following topology.

±+ #2 ±+ #1 ±+
|A±------------>|C±--------->|D|
+++ +++ ±+
| |
| |
| |
| |
| |
| |
#1| |
| |#2
| |
| |
| |
v |
±+ |
|B|<--------------+
±+
Router A has two out-edges, its #1 out edge is connected to Router B and its #2 out edge is connected to Router C.
Router C has two out-edges, too. Its #1 out edge is connected to Router D and its #2 out edge is connected to Router B.
Suppose A is going to send a packet to Router D. The only path in this graph is A->C->D. The link used is the #2 out edge of A, and the #1 out edge of C.
A is going to receive a list of instructions along with the packet if segment routing protocol is enabled:
The first instruction is to forward the package by #2 out-edge of the current vertex (Router A), the second instruction is to forward the package by #1 out-edge of the current vertex (Router C). Hence, the routers will check the instructions in the packet header to decide the next hop. The routing table looking up is no longer needed.
Now we have a network log recording the SRv6 packet headers used during last month. Unfortunately, the destination addresses get lost, can you recover them from the forward instructions?
Input
There are T test cases in this problem.
The first line has one integer T.
For each test case:
The first line has two integers N and M. N denotes the number of vertices in the graph, i.e. the number of routers in the network. Routers are labelled from 1 to N. M is the number of packet records in the log file.
In the next N lines, the i-th line begin with an integer D i , meaning that Router i has D i outgoing edge(s). In the following D i​ integers, the j-th integer O ij indicates that there is an edge from Router i to Router O ij
​ , and this edge is labelled as the j-th outgoing edge of Router i.In the next M lines, the i-th line begins with two numbers S i​ and L i​ . S i​indicates the source router of the i-th packet. L i indicates the total number of edges used for this packet. Followed by L i​ integers, the j-th integer R ij​ indicates the j-th router in the forwarding path uses the R ij​
-th outgoing edge to forward the packet.
在这里插入图片描述
Output
For each test case, you should first print a line "Case #t: " (without quotes, with an ending space), where t is the index of this test case, then output M lines.
If a packet could reach a router followed by the route given by the i-th record in the log, the i-th output line should contain one integer T i​ , indicating the destination node for the i-th record.
Otherwise, if the route given by the i-th recorder is illegal, output “Packet Loss” (without quotes). For instance, the header may instruct the i-th router to use the 10-th outgoing edge for forwarding while router i only has 8 outgoing edges.

题意:
给定T组询问,每组询问给定一个n和m,接下来n行每行给定一个cnt然后输入cnt个点,表示当前行和输入的u之间有一条有向边,然后接下来的m行每行输入一个now 和 cnt 表示从now这个点开始走cnt次,每一次输入一个ttt,表示从now这个点走的第ttt个边,如果不存在这个出"Packet Loss",否则u就走到这个点,最后输出能到的哪个点。在这个过程中不要碰到到不了的点就退出,要把这组的数据全部输入进来

题解:
开一个二维vector来存当前这个点能到的所有的点即可

#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
#define ll long long
vector <int > s[100005];
int main()
{
    int t;
    cin>>t;
    for(int i=1; i<=t; i++)
    {
        printf("Case #%d: \n",i);
        int n,m;
        scanf("%d%d",&n,&m);
        for(int j=1; j<=n; j++)
        {
            s[j].clear();
            int cnt=0;
            scanf("%d",&cnt);
            for(int k=1; k<=cnt; k++)
            {
                int u;
                scanf("%d",&u);
                s[j].push_back(u);
            }
        }
        for(int j=1; j<=m; j++)
        {
            int pan=0;
            int now,cnt=0;
            scanf("%d%d",&now,&cnt);
            int vis=now;
            int ttt;
            for(int k=1; k<=cnt; k++)
            {
                scanf("%d",&ttt);
                if(ttt==0||ttt>s[vis].size())
                    pan=1;
                if(pan)
                    continue;
                vis=s[vis][ttt-1];
            }
            if(pan!=0)
                cout << "Packet Loss" << endl;
            else
                cout << vis << endl;


        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prime me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值