[ZJOI2004]嗅探器

题目

点我

题解1

挺水的题, 枚举割点在判断S->T是否联通即可

CODE1

但下面的代码无疑会超时

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;   
typedef long long ull;
typedef pair <int, int > pii;

inline int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    return s * w;
}

const int inf = 0x3f3f3f3f;
int n, a, b;
struct Edge { int next, to; } edge[maxn];
int num_edge = 0, tim = 0, ss = 0, s, e;
int head[maxn], low[maxn], dfn[maxn], cnt[maxn], vis[maxn];
int t[maxn], point = inf;
bool flag = false;

inline void add(int from, int to) {
    edge[++num_edge].to = to;
    edge[num_edge].next = head[from];
    head[from] = num_edge;  
}

void dfs(int u, int fa) {
    dfn[u] = low[u] = ++tim;
    int son = 0;
    for (int i = head[u]; i; i = edge[i].next) {
        int v = edge[i].to;
        if (!dfn[v]) {
            dfs(v, fa);
            low[u] = min(low[u], low[v]);
            if (low[v] >= dfn[u] && u != fa) { 
                cnt[u] = 1; 
            }
            if (u == fa) son++;
        }
        else low[u] = min(low[u], dfn[v]);
    }
    if (son >= 2 && u == fa) cnt[u] = 1;
}

bool check(int k) {
    queue<int> q;
    // cout << "11111" << endl;
    memset(vis , 0, sizeof(vis));
    vis[s] = 1; q.push(s);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i = head[u]; i; i = edge[i].next) {
            int v = edge[i].to;
            // cout << v << endl;
            if (v == e) return false;
            if (v == k) continue;
            if (!vis[v]) {
                q.push(v);
                vis[v] = 1;
            }
        }
    }
    return true;
}

int main() {
    // freopen("dfnIn.in","r",stdin);
    // freopen("dfnin.out","w",stdout);
    n = read();
    while (scanf("%d %d", &a, &b)) {
        if (a == 0 && b == 0) break;
        add(a, b);
        add(b, a);
    }
    s = read(), e = read();
    for (int i = 1; i <= n; ++i) {
        if (!dfn[i]) dfs(i, i);
    }
    for (int i = 1; i <= n; ++i) if (cnt[i] > 0) t[++ss] = i;
    // for (int i = 1; i <= ss; ++i) printf("%d ", t[i]);
    // printf("\n");
    for (int i = 1; i <= ss; ++i) {
        if (check(t[i])) {
            // cout << t[i] << endl;
            printf("%d\n", t[i]);
            flag = true;
            system("PAUSE");
            return 0;
        }
    }
    if (!flag) printf("No solution\n");
    // system("PAUSE");
    return 0;    
}

题解2

考虑我们有一个u点,它连了一个v点,那么u点需要满足4个条件。

1、u不是起点终点。因为题目要求在中间服务器上建嗅探器。
2、u是割点。废话
3、终点(y)的dfn应该大于等于v点的dfn,因为要确保终点在v点或之后被访问到,即u点为必经的点。
4、终点(y)的low应该大于等于u点的dfn,因为要确保终点必须要经过u点。

code2

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;   
typedef long long ull;
typedef pair <int, int > pii;
const int inf = 0x3f3f3f3f;

inline int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    return s * w;
}

int n, a, b;
int ans = inf;
struct Edge { int next, to; } edge[maxn];
int num_edge = 0, tim = 0, ss = 0, s, e;
int head[maxn], low[maxn], dfn[maxn], cnt[maxn], vis[maxn];
int t[maxn], point = inf;
bool flag = false;

inline void add(int from, int to) {
    edge[++num_edge].to = to;
    edge[num_edge].next = head[from];
    head[from] = num_edge;  
}

void tarjan(int u) {
    dfn[u] = low[u] = ++tim;
    for (int i = head[u]; i; i = edge[i].next) {
        int v = edge[i].to;
        if (!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
            if (u != s && u != e) {
            	if (dfn[u] <= low[v] && dfn[v] <= dfn[e] && low[e] >= dfn[s]) {
            		ans = min(ans, u);
				}
			}
        }
        else low[u] = min(low[u], dfn[v]);
    }
}

int main() {
    n = read();
    while (scanf("%d %d", &a, &b)) {
        if (a == 0 && b == 0) break;
        add(a, b);
        add(b, a);
    }
    s = read(), e = read();
    tarjan(s);
    if (ans == inf) printf("No solution\n");
    else printf("%d\n", ans);
	return 0;    
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于嗅探器的源代码#include <winsock2.h> #include <windows.h> #include <ws2tcpip.h> #include <stdio.h> #include <stdlib.h> #pragma comment(lib,"ws2_32.lib") #define MAX_HOSTNAME_LAN 255 #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1) #define MAX_ADDR_LEN 16 struct ipheader { unsigned char ip_hl:4; unsigned char ip_v:4; unsigned char ip_tos; unsigned short int ip_len; unsigned short int ip_id; unsigned short int ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short int ip_sum; unsigned int ip_src; unsigned int ip_dst; }; typedef struct tcpheader { unsigned short int sport; unsigned short int dport; unsigned int th_seq; unsigned int th_ack; unsigned char th_x:4; unsigned char th_off:4; unsigned char Flags; unsigned short int th_win; unsigned short int th_sum; unsigned short int th_urp; }TCP_HDR; typedef struct udphdr { unsigned short sport; unsigned short dport; unsigned short len; unsigned short cksum; }UDP_HDR; void main(){ SOCKET sock; WSADATA wsd; DWORD dwBytesRet; unsigned int optval = 1; unsigned char *dataudp,*datatcp; int i,pCount=0,lentcp, lenudp; SOCKADDR_IN sa,saSource, saDest; struct hostent FAR * pHostent; char FAR name[MAX_HOSTNAME_LAN]; char szSourceIP[MAX_ADDR_LEN], szDestIP[MAX_ADDR_LEN],RecvBuf[65535] = {0}; struct udphdr *pUdpheader; struct ipheader *pIpheader; struct tcpheader *pTcpheader; WSAStartup(MAKEWORD(2,1),&wsd); if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR) exit(1); gethostname(name, MAX_HOSTNAME_LAN); pHostent = gethostbyname(name); sa.sin_family = AF_INET; sa.sin_port = htons(6000); memcpy(&sa.sin_addr.S_un.S_addr, pHostent->h_addr_list[0], pHostent->h_length); bind(sock, (SOCKADDR *)&sa, sizeof(sa)); if ((WSAGetLastError())==10013) exit(1); WSAIoctl(sock, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &dwBytesRet, NULL, NULL); pIpheader = (struct ipheader *)RecvBuf; pTcpheader = (struct tcpheader *)(RecvBuf+ sizeof(struct ipheader )); pUdpheader = (struct udphdr *) (RecvBuf+ sizeof(struct ipheader )); while (1){ memset(RecvBuf, 0, sizeof(RecvBuf)); recv(sock, RecvBuf, sizeof(RecvBuf), 0); saSource.sin_addr.s_addr = pIpheader->ip_src; strncpy(szSourceIP, inet_ntoa(saSource.sin_addr), MAX_ADDR_LEN); saDest.sin_addr.s_addr = pIpheader->ip_dst; strncpy(szDestIP, inet_ntoa(saDest.sin_addr), MAX_ADDR_LEN); lentcp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct tcpheader))); lenudp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct udphdr))); if((pIpheader->ip_p)==IPPROTO_TCP&&lentcp!=0){ printf("*******************************************\n"); pCount++; datatcp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct tcpheader); printf("-TCP-\n"); printf("\n%s\n",szDestIP); printf("\n%i\n",ntohs(pTcpheader->dport)); printf("datatcp address->%x\n",datatcp); printf("size of ipheader->%i\n",sizeof(struct ipheader)); printf("size of tcpheader->%i\n",sizeof(struct tcpheader)); printf("size of the hole packet->%i\n",ntohs(pIpheader->ip_len)); printf("\nchar Packet%i [%i]=\"",pCount,lentcp-1); for (i=0;i<lentcp;i++){ printf("\\x%.2x",*(datatcp+i)); if (i==0) printf("\"\n\""); } printf("\";\n\n\n"); for (i=0;i<lentcp;i++){ if( *(datatcp+i)<=127&&*(datatcp+i)>=20) printf("%c",*(datatcp+i)); else printf("."); } printf("\n\n*******************************************\n"); } if((pIpheader->ip_p)==IPPROTO_UDP&&lentcp!=0){ pCount++; dataudp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct udphdr); printf("-UDP-\n"); printf("\n%s\n",szDestIP); printf("\n%d\n",ntohs(pTcpheader->dport)); printf("UDP%x\n",dataudp); printf("IP%i\n",sizeof(struct ipheader)); printf("UDP%i\n",sizeof(struct udphdr)); printf("%i\n",ntohs(pIpheader->ip_len)); printf("\nchar Packet%i [%i]=\"",pCount,lenudp-1); for (i=0;i<lenudp;i++){ printf("\\x%.2x",*(dataudp+i)); if (i==0) printf("\"\n\""); } printf("\";\n\n\n"); for (i=0;i<lenudp;i++){ if( *(dataudp+i)<=127&&*(dataudp+i)>=20) printf("%c",*(dataudp+i)); else printf("."); } printf("\n\n*******************************************\n"); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值